previous | start | next

Implementing Linked Lists (Implementing Insertion and Removal)

Of course we cannot remove a node that is not there.

assert(iter.position != NULL)

We create pointers to track all three nodes we need to work with.

Node* remove = iter.position;
Node* before = remove->previous;
Node* after = remove->next;

We disconnect the node to be removed from the one before it; note the special case when we delete from the front of the list.

if (remove == first)
   first = after;
else
   before->next = after;

We repeat the process with the node after the one to delete.

if (remove == last)
   last = before;
else
   after->previous = before;

Finally we delete the node.

delete remove;


previous | start | next