In this blog post will study what is doubly linked lists, its
applications, operations and implementation in C++.
Please check the blog post if you don't know what is
Linked Lists.
Definition
Doubly linked lists are two-way lists because we can traverse
in both the directions that is from head node to end or in
reverse direction.
Each node in DLL contains 1 data field and 2 reference fields.
In these 2 reference feilds, there is one pointer reference to
previous node and another to next node in the list.
As we have 2 pointers, we can traverse in two directions
starting from the head node to the end and vice versa.
Applications
- Previous and next page in web browser.
- Undo and redo in editors.
- DNA molecules.
-
Applications that have a most recently used/search list.
-
Music players in which you can play the previous or next song
in the list.
Operations
Do refer the code available the end of this section to
understand the following thoery.
Note that the head node's data is used to count the no. of nodes
in the list & it's next pointer points to the first node. The
previous pointer of head and the next pointer all the tail nodes
will always point to NULL.
1. Traverse: Iterate through the nodes in the
linked list starting from the head node.
-
Let us assume that the head points to the first node of the
list.
-
Create a temporary node which will point to the same node as
that of head.
-
Follow the pointers and stop when the next pointer points to
NULL.
2. Append: Add a new node to the end of a
list.
-
Create a temporary pointer which points to current head node
and traverse to the end of the list.
-
Make the new node's right pointer, to point to NULL and left
pointer, to point to the last node.
-
Update right pointer of last node to point to new node.
3. Prepend: Add a new node to the beginning
of a list.
-
Right pointer of head node, left & right pointer of new node
need to be modified and it can be done in two steps.
-
Update the right pointer of the new node to point to the
current head's next node and also make left pointer of new
node to point to head.
-
Update head node’s right pointer to point to the new node.
4. Insert by position : Add a new node to a
specific position on the list.
-
If we want to add an element at position 3 then we stop at
position 2. That means we traverse 2 nodes and insert the
new node. For simplicity let us assume that the second node
is called position node.
-
New node right pointer points to the next node of the
position node and new node left pointer points to the
position node.
-
Position node right pointer points to the new node and the
next node of position node left pointer points to new node.
5. Delete at front: Remove the first node
from the list.
-
Move the head node's pointer to the next node and change the
head node's left pointer to NULL.
- The front node will be unlinked with the list.
5. Delete at rear: Remove the last node from
the list.
-
Traverse the list and while traversing maintain the previous
node address also. By the time we reach the end of the list,
we will have two pointers, one pointing to the tail and the
other pointing to the node before the tail.
-
Update the next pointer of previous node to the tail node
with NULL and the rear node will be unlinked with the list.
5. Delete by position: Remove a node at
specified position from the list.
-
The node to be removed is always located between two nodes.
The head and tail links are not updated.
-
Similar to the previous case, maintain the previous node
while also traversing the list. Upon locating the node to be
deleted(position node), change the previous node’s right
pointer to the right pointer of the node to be deleted.
-
Update the left pointer of the node(which is the right node
of the node to be deleted) to point to the previous node(of
the node to be deleted). Bit clumsy right? :(
8. Display: Traverse and display the data of
all nodes starting from head node.
-
Traverse the linked list(1st operation) and at each node,
display its content
-
This traversing method can also be used to count the number
of nodes in the linked list.
You can do all the operations mentioned above with one
temporary pointer itself.