Chapter 13: Lists, Stacks, and Queues

Lecture Goal

Linked Lists

Linked Lists(list1.cpp)


Implementing Linked Lists (The Classes for Lists, Nodes, and Iterators)

Implementing Linked Lists (Implementing Iterators)

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;

Implementing Linked Lists (list2.cpp)