Linked-list destructor crashes C++ program -



Linked-list destructor crashes C++ program -

i've been working on destructor linked list project numbers inserted in front end , odd numbers inserted in back. deletion works on lastly in first out depending on if odd or even. have working maintain getting error when run destructor. i've walked through debug , deletes of nodes crashes afterwards , i'm not sure why. here code:

//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; { cout << "would to: \nadd node: 1\ndelete node: 2\ndisplay list: 3\nquit: 0\n"; cin >> choice; while (choice < 0 || selection > 3) { cout << "invalid input: to: \nadd node: 1\ndelete node: 2\ndisplay list: 3\nquit: 0\n"; cin >> choice; } switch (choice) { case 1: cout << "enter value add together list: "; cin >> input; while (isalpha(input)) { cout << "invalid input: come in value add together list: "; cin >> input; } mylist.addnode(input); mylist.display(); break; case 2: cout << "enter number of numbers delete: "; cin >> numeven; while (isalpha(input) || numeven < 0) { cout << "invalid input: come in number of numbers delete: "; cin >> numeven; } cout << "enter number of odd numbers delete: "; cin >> numodd; while (isalpha(input) || numeven < 0) { cout << "invalid input: come in number of odd numbers delete: "; cin >> numodd; } mylist.deletenode(numodd, numeven); mylist.display(); break; case 3: mylist.display(); break; default: break; } } while (choice != 0); mylist.~staque(); homecoming 0; }

one obvious error you're calling destructor explicitly.

mylist.~staque();

you shouldn't -- object "die" naturarlly when goes out of scope. calling destructor explicitly, object have destructor called, destructor called sec time when object goes out of scope. sec invocation cause havoc break loose.

the time when should phone call destructor explicitly if you're using placement-new, , you're not doing here. hence remove line above , see if error goes away. if doesn't, have farther problems, @ least, remove glaring issue code.

c++ linked-list destructor delete-operator

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -