samedi 27 juin 2015

Deleting a node from the start of a linked list

I am implementing a stack using a singly linked list where the Head is at the top of the stack and the Tail is at the bottom of the stack.

I am implementing a pop operation. In order to do this, I must make the head equal to the second node in the linked list. But before I do this, I need to delete the first node in the linked list first. I can do this using delete head->next;.

My question is, if I delete that first node, can I still use it to move on to the next node? Or is it bad practice to use a reference of a node that has had delete called on it. Here is the code I want to use to implement pop.

delete head->next;
head->next = head->next->next;

Aucun commentaire:

Enregistrer un commentaire