链表析构函数使C++程序崩溃



我一直在为一个链表项目开发析构函数,其中偶数插入在前面,奇数插入在后面。删除对后进先出有效,这取决于它是奇数还是偶数。我有所有的工作,但当我运行我的析构函数时,我不断地得到一个错误。我已经完成了调试,它删除了所有节点,但随后崩溃,我不知道为什么。这是我的代码:

//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: 1nDelete a node: 2nDisplay the list: 3nQuit: 0n";
    cin >> choice;
    while (choice < 0 || choice > 3)
    {
        cout << "Invalid Input: Would you like to: nAdd a node: 1nDelete a node: 2nDisplay the list: 3nQuit: 0n";
        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;
}
一个明显的错误是显式调用析构函数。
myList.~Staque();

你不应该这样做——当对象超出范围时,它会自然地"死亡"。通过显式调用析构函数,将调用对象的析构函数。然后,当对象超出范围时,将再次调用析构因子。这是第二次调用,将导致所有浩劫的爆发。

应该显式调用析构函数的时间是如果使用placement-new,而这里没有这样做。因此,只需删除上面的那一行,看看错误是否消失。如果没有,那么你会遇到更多的问题,但至少,你会从代码中删除这个明显的问题。

具体查看析构函数。。。

当nodePtr为null时会发生什么?例如,你没有创建一个节点。

或者你到达最后一个节点,它总是将下一个ptr设置为NULL,然后你在一个NULL指针上取消引用nodePtr->next——是的,你猜这是一个崩溃。

在您的时间(nodePtr->next),临时分配在做什么?似乎没什么用。

而(nodePtr->prev)???

没有检查nodePtr为空?

这是一个开始。

你确定其余的都有效吗?

相关内容

  • 没有找到相关文章

最新更新