-
deleting a linked list
i'm hoping i'm wrong here but to delete a linked list you have to make a delete command on each the pointer for each node yes?
so does that mean that to fully delete a linked list you actually need to have stored somehow the pointers for the node that comes above the node your deleting (if you start at the last node, the 'bottom' of the list) or does anyone know a better way i'm not thinking of?
-
You could delete it top down, as long as you remember to store the next node for moving up before deleting the existing top.
-
Code:
class Node
{
private:
int data;
Node* next;
friend class List;
};
class List
{
public:
//functions and stuff
private:
Node* head;
Node* tail;
};
Assuming your list is like those I have encountered you would normally delete each node via the link (called next in Node class). The code will be kinda like this:
Code:
void List::~List( )
{
Node* toGo; //pointer to node to be deleted
while( head != tail )
{
toGo = head;
head = head->next;
delete toGo;
}
}
-
duh...i can be such a donut once it hits midnight. need coffee...