thesleepylizard
Programmer
Seems to me that an easy way of deleting linked lists is like this:
Only downside to this is that each successive delete command will add another entry to the stack, and if you have thousands of linked lists this will be quite a lot of stack work, and certainly not suitable for an enterprise situation. OTOH, IMO linked lists shouldn't BE used for thousands of entries.
Any thoughts on this?
Cheers,
-lizard
Code:
struct Item {
Item () {
Next = NULL;
}
~Item () {
if (Next) delete Next; //...which calls the destructor for the next object, which call the destructor for... etc
}
Item* MakeNext () {
return Next = new Item(); //This is just a nicety too
}
Item* Next;
int Member1;
int Member2;
};
CurrentItem = FirstItem = new Item();
CurrentItem = CurrentItem->MakeNext();
CurrentItem = CurrentItem->MakeNext();
CurrentItem = CurrentItem->MakeNext();
CurrentItem = CurrentItem->MakeNext();
delete FirstItem; //Flies through ALL items!
Only downside to this is that each successive delete command will add another entry to the stack, and if you have thousands of linked lists this will be quite a lot of stack work, and certainly not suitable for an enterprise situation. OTOH, IMO linked lists shouldn't BE used for thousands of entries.
Any thoughts on this?
Cheers,
-lizard