Doubly Linked Lists
Overview!
-Assume we have a linked list, and now we want to append a node to the end of the list.
What we can do to make it happen? [We need to have a temporary, use it to go through the
list, and make it points at the last node of the list. Then update the next pointer of the last
node and set it points to the new node.] !
-This means if we have a long list, it will take a while to find the last node and do the
operation. Same thing happens to removing the last node. The time complexity depends on
the size of the list. !
-How can we modify the linked list, so that it will be easier to access the tail of the list?!
•Add a tail pointer.!
-Or we can make it even more convenient in a way that we can move forward and backward
in the list. To achieve this, we can add a prev pointer to node in addition to the next pointer. !
-And this is what we called doubly linked lists. !
-Pros: more flexible !
-Cons: more memory and maintenance of references needed!
-Note: Header and trailer are sentinel nodes. A sentinel node is a specifically designed node
used with linked lists and trees as a traversal path terminator. This type of node does not
hold or reference any data managed by the data structure.!
-Each node in a doubly linked list contains three fields: the data, and two pointers: prev and
next.!
Private inner class Node !
private class Node {
private Node prev;
private Node next;
private T data;