Linked list destructor
Hey guys, creating myself a linked list here, and hoping to be able to make it generic and enable it to be used anywhere :)
ANYWAY, i'm just looking at the list destructor. If the list contains nodes, with pointers to other nodes etc, do i need to delete all of the nodes separately to deallocate all of the memory correctly, or can i simply delete the list and the nodes should all go with it?
Are you working in C++? If so, you need a delete for every new. If you're working in Java, it'll be GCed automatically when all the references to it are gone. If you're working in C -- again, a free() for every malloc().
P.S. -- I highly recommend creating your own linked list and using it somewhere, at least once. But once you have done that, move over to a standard list implementation; they're usually much more robust.
P.S. -- I highly recommend creating your own linked list and using it somewhere, at least once. But once you have done that, move over to a standard list implementation; they're usually much more robust.
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P
Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's
Anyway, cheers mate
Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's
Anyway, cheers mate
Quote:Original post by Welshy
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P
Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's
Anyway, cheers mate
Awesome. That's how I all of a sudden came to understand memory managament in C++ -- creating a linked list. I like you, so I'd give you the delete algorithm, but you'll learn more implementing it yourself.
Ciao,
Twilight Dragon
Quote:Original post by TDragonQuote:Original post by Welshy
I'm using C++. Alright, now to go make an algorithm to delete all nodes :P
Yeah i saw a linked list in a couple of books i've read/flicked through, so i thought it'd be a good challenge to create my own, and it has been very educational, especially in using pointers, 'new's and 'delete's
Anyway, cheers mate
Awesome. That's how I all of a sudden came to understand memory managament in C++ -- creating a linked list. I like you, so I'd give you the delete algorithm, but you'll learn more implementing it yourself.
Ciao,
Twilight Dragon
lol, thanks. Well i'll be at it all evening (i hope it doesnt take that long, though it probably will), i'll post it up once i've got it working and you can criticise it :P
CList::~CList() // destructor{ CNode *pPointer; pPointer = pRoot; // sets the pointer to start at the beginning of the list do { CNode *pPointerDel; // the delete pointer pPointerDel = pPointer; delete pPointerDel; if(pPointer->pNext != 0) { pPointer = pPointer->pNext; // sets pointer to the next node in the list } } while(pPointer != pHead);}
Here's what i've got, and it should work (i think).
But it doesnt run, so for pPointerDel to delete the node it's pointing to do i need to overload the assignment operator or something? i remember reading about it. Im thinking this could be the problem because when i set a breakpoint at the node destructor (which doesnt contain any code, yet) and it cannot resolve the values of the member variables, which im guessing means that the pointer doesnt contain all of the node information?
Remember, if pointer a and pointer b point at the same thing, and either is deleted, both are then invalid [since the memory they point to is free'd/delete'd]
Quote:Original post by Welshy
Then how would you keep track of where you are in the deletion process?
That's part of the fun!
There's probably 3-4 ways to do it.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement