| | |
Destructor Function for List structure
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
For a given list, I would like my output to have the line "Deleting node with value ..." for each node.
My destructor function works for a 2-element list, but for a 3-element list it deletes a certain node more than once, and for a list of any size greater than 3, I get an infinite loop. I try tracing through the code, but I am not sure what is going on. Any suggestions? Thanks.
My destructor function works for a 2-element list, but for a 3-element list it deletes a certain node more than once, and for a list of any size greater than 3, I get an infinite loop. I try tracing through the code, but I am not sure what is going on. Any suggestions? Thanks.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cassert> #include "lists.h" using namespace std; ListNode::ListNode (int k) { myValue = k; myNext = 0; } ListNode::ListNode (int k, ListNode* ptr) { myValue = k; myNext = ptr; } ListNode::~ListNode () { cout << "Deleting node with value " << myValue << endl; for (ListNode* p=this; p!=0; ){ p=p->myNext; delete p; } }
Last edited by Narue; Jan 14th, 2008 at 10:25 am. Reason: Added code tags
C++ Syntax (Toggle Plain Text)
for (ListNode* p=this; p!=0; ){ p=p->myNext; delete p; }
I'm here to prove you wrong.
•
•
Join Date: Jan 2008
Posts: 4
Reputation:
Solved Threads: 0
I'm actually working with just one class called ListNode. I changed my destructor function to look like this.
ListNode::~ListNode () {
cout << "Deleting node with value " << myValue << endl;
//this automatically stops when p points to 0, seems to be just stepping through the list?
ListNode* p = this;
p=p->myNext;
delete p;
}
It works now, but I'm not completely sure why. It seems like the function is simply stepping through the list, but when I view the contents of the list after the deletion, there are now garbage values in all the nodes. Given that I am only working with one class, is this the right way to do it? Thanks.
ListNode::~ListNode () {
cout << "Deleting node with value " << myValue << endl;
//this automatically stops when p points to 0, seems to be just stepping through the list?
ListNode* p = this;
p=p->myNext;
delete p;
}
It works now, but I'm not completely sure why. It seems like the function is simply stepping through the list, but when I view the contents of the list after the deletion, there are now garbage values in all the nodes. Given that I am only working with one class, is this the right way to do it? Thanks.
![]() |
Similar Threads
- help with this code of link list (C++)
- Looking up and displaying values in linked lists (C++)
- new member here :) (C)
Other Threads in the C++ Forum
- Previous Thread: String class
- Next Thread: Programming student find distances btwn cities using array of structs
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






