我一直在为我的析构函数开发一个链表项目,其中偶数插入前面,奇数插入后面。删除工作在后进先出,取决于它是奇数还是偶数。我有一切正常,但是当我运行析构函数时,我一直收到错误。我已经完成了调试并删除了所有节点,但随后崩溃了,我不知道为什么。这是我的代码:
//Headerfile
class Staque
{
private:
struct staqueNode
{
int value;
struct staqueNode *next;
struct staqueNode *prev;
};
staqueNode *root;
public:
Staque();
~Staque();
void addNode(int addIn);
void deleteNode(int oddIn, int evenIn);
void display();
};
//header.cpp file
#include"Staque.h"
#include<iostream>
using namespace std;
Staque::Staque()
{
root = NULL;
}
void Staque::addNode(int addIn)
{
staqueNode *newNode;
staqueNode *nodePtr;
staqueNode *temp = NULL;
newNode = new staqueNode;
newNode->value = addIn;
newNode->next = NULL;
newNode->prev = NULL;
if (!root)
{
root = newNode;
}
else
{
if (newNode->value % 2 == 0)
{
nodePtr = root;
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
nodePtr->next = newNode;
newNode->prev = nodePtr;
}
else if (newNode->value % 2 != 0)
{
nodePtr = root;
while (nodePtr->prev)
{
nodePtr = nodePtr->prev;
}
nodePtr->prev = newNode;
newNode->next = nodePtr;
}
}
}
void Staque::deleteNode(int oddIn, int evenIn)
{
staqueNode *nodePtr;
staqueNode *temp = NULL;
if (!root)
return;
while (evenIn > 0)
{
nodePtr = root;
while (nodePtr != NULL && nodePtr->next != NULL)
{
temp = nodePtr;
nodePtr = nodePtr->next;
}
if (nodePtr == root && root->value % 2 == 0)
{
root = root->prev;
temp->next = NULL;
delete nodePtr;
evenIn = 0;
}
else
{
temp->next = NULL;
delete nodePtr;
evenIn -= 1;
}
}
while (oddIn > 0)
{
nodePtr = root;
while (nodePtr != NULL && nodePtr->prev != NULL)
{
temp = nodePtr;
nodePtr = nodePtr->prev;
}
if (nodePtr == root && root->value % 2 != 0)
{
root = root->next;
temp->prev = NULL;
delete nodePtr;
oddIn = 0;
}
else
{
temp->prev = NULL;
delete nodePtr;
oddIn -= 1;
}
}
}
void Staque::display()
{
staqueNode *nodePtr;
nodePtr = root;
while (nodePtr->next)
{
nodePtr = nodePtr->next;
}
cout << "\nThe staque: ";
while (nodePtr->prev)
{
cout << nodePtr->value << " ";
nodePtr = nodePtr->prev;
}
cout << nodePtr->value << endl;
}
Staque::~Staque()
{
staqueNode *nodePtr;
staqueNode *temp;
nodePtr = root;
while (nodePtr->next)
{
temp = nodePtr;
nodePtr = nodePtr->next;
}
//nodePtr = root;
while (nodePtr->prev)
{
temp = nodePtr;
nodePtr = nodePtr->prev;
delete temp;
}
//delete root;
}
//source/main.cpp
#include"Staque.h"
#include<iostream>
using namespace std;
int main()
{
Staque myList;
int choice;
int input;
int numOdd;
int numEven;
do
{
cout << "Would you like to: \nAdd a node: 1\nDelete a node: 2\nDisplay the list: 3\nQuit: 0\n";
cin >> choice;
while (choice < 0 || choice > 3)
{
cout << "Invalid Input: Would you like to: \nAdd a node: 1\nDelete a node: 2\nDisplay the list: 3\nQuit: 0\n";
cin >> choice;
}
switch (choice)
{
case 1:
cout << "Enter the value you would like to add to the list: ";
cin >> input;
while (isalpha(input))
{
cout << "Invalid input: Enter the value you would like to add to the list: ";
cin >> input;
}
myList.addNode(input);
myList.display();
break;
case 2:
cout << "Enter the number of even numbers you would like to delete: ";
cin >> numEven;
while (isalpha(input) || numEven < 0)
{
cout << "Invalid input: Enter the number of even numbers you would like to delete: ";
cin >> numEven;
}
cout << "Enter the number of odd numbers you would like to delete: ";
cin >> numOdd;
while (isalpha(input) || numEven < 0)
{
cout << "Invalid input: Enter the number of odd numbers you would like to delete: ";
cin >> numOdd;
}
myList.deleteNode(numOdd, numEven);
myList.display();
break;
case 3:
myList.display();
break;
default:
break;
}
} while (choice != 0);
myList.~Staque();
return 0;
}
答案 0 :(得分:1)
一个明显的错误是你明确地调用了析构函数。
myList.~Staque();
你不应该这样做 - 当物体超出范围时,物体将自然地“死亡”。通过显式调用析构函数,该对象将调用析构函数,然后在对象超出作用域时第二次调用析构函数。这是第二次调用会导致所有破坏。
你应该明确调用析构函数的时间是你正在使用placement-new
,而你在这里没有这样做。因此,只需删除上面的那一行,看看错误是否消失。如果没有,那么您还有其他问题,但至少,您会从代码中删除这个明显的问题。
答案 1 :(得分:0)
专门看你的析构函数......
nodePtr为null时会发生什么?例如,您没有创建节点。
或者你到达最后一个节点,它总是将下一个ptr设置为NULL,然后在空指针上取消引用nodePtr-&gt; - 是的,你猜它是一个崩溃。
在你的while(nodePtr-&gt; next)什么是临时任务呢?看起来没什么用。
while(nodePtr-&gt; prev)???
不检查nodePtr是否为空?
首先要做的事情。
您是否确定其余部分有效?