我一直在研究我的析構函數,用於鏈接列表項目,其中偶數插入前面,奇數插入後面。取決於它是奇數還是偶數,刪除在最後進行中起作用。我有一切工作,但我運行我的析構函數時不斷收到錯誤。我已經完成了調試,它刪除了所有的節點,但之後崩潰,我不知道爲什麼。這裏是我的代碼:鏈接列表析構函數崩潰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: 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;
}
什麼是錯誤? – Celeo 2014-10-20 21:16:24
'我有一切工作,但當我運行我的析構函數時,我總是收到一個錯誤'這可能意味着整件事*不*工作。另外,請修正您的格式。 – PaulMcKenzie 2014-10-20 21:17:54
我懷疑你沒有遵守[Rule of Three](http://stackoverflow.com/q/4172722/10077)。 – 2014-10-20 21:18:29