TopAbstractList classVariants of linked lists

Variants of linked lists

We can improve the behavior of linked lists on addLast and removeLast, as well as simplify some code by creating a doubly-linked list in which we store references to both the front and the back of the list. To do this we first replace SinglyLinkedListElement by DoublyLinkedListElement. This differs from the previous one by keeping links to both the previous and next nodes in the list. The doubly linked list is composed of these more complicated nodes and keeps instance variables for both the head and tail of the list.

One advantage of a doubly linked list is that you need not maintain a pointer to the element before the one you wish to delete because you can get to that element using the previous pointer.

Because we have a pointer to both the head and tail of the list, it is easy to add an element to the tail - making it O(1) rather than O(n). Because the nodes are doubly linked we can also efficiently remove the last element (also O(1)). Note that this would not be possible with only a singly linked list as it would take O(n) time to update the tail pointer after the deletion. See the classes DoublyLinkedListElement and DoublyLinkedList in the above-linked code.


TopAbstractList classVariants of linked lists