What is Doubly Linked List?
• Doubly Linked List is a variation of Linked list in which navigation is possible
in both ways, forward as well as backward easily as compared to Single
Linked List. Following are the important terms to understand the concept
of doubly linked list.
• Link − Each link of a linked list can store a data called an element.
• Next − Each link of a linked list contains a link to the next link called Next.
• Prev − Each link of a linked list contains a link to the previous link called
Prev.
• Linked List − A Linked List contains the connection link to the first link
called First and to the last link called Last.
Doubly Linked List Representation
As per the above illustration, following are the important points to be considered.
•Doubly Linked List contains a link element called first and last.
•Each link carries a data field(s) and a link field called next.
•Each link is linked with its next link using its next link.
•Each link is linked with its previous link using its previous link.
•The last link carries a link as null to mark the end of the list
Basic Operations in Doubly Linked List
• Following are the basic operations supported by a list.
• Insertion − Adds an element at the beginning of the list.
• Insert Last − Adds an element at the end of the list.
• Insert After − Adds an element after an item of the list.
• Deletion − Deletes an element at the beginning of the list.
• Delete Last − Deletes an element from the end of the list.
• Delete − Deletes an element from the list using the key.
• Display forward − Displays the complete list in a forward manner.
• Display backward − Displays the complete list in a backward manner.
Doubly Linked List - Insertion at the Beginning
• In this operation, we create a new node with three compartments, one
containing the data, the others containing the address of its previous and next
nodes in the list. This new node is inserted at the beginning of the list.
• Algorithm
• 1. START
• 2. Create a new node with three variables: prev, data, next.
• 3. Store the new data in the data variable
• 4. If the list is empty, make the new node as head.
• 5. Otherwise, link the address of the existing first node to the next variable of the
new node, and assign null to the prev variable.
• 6. Point the head to the new node.
• 7. END
Doubly Linked List - Insertion at the End
• Algorithm
• 1. START
• 2. If the list is empty, add the node to the list and point the head to it.
3. If the list is not empty, find the last node of the list.
• 4. Create a link between the last node in the list and the new node.
• 5. The new node will point to NULL as it is the new last node.
• 6. END
Doubly Linked List - Deletion at the Beginning
• This deletion operation deletes the existing first nodes in the doubly
linked list. The head is shifted to the next node and the link is
removed.
• Algorithm
• 1. START
• 2. Check the status of the doubly linked list
• 3. If the list is empty, deletion is not possible
• 4. If the list is not empty, the head pointer is shifted to the next node.
5. END
Deletion at the End of Doubly Linked List
• To delete a node at the end in doubly linked list, we can use the
following steps:
• Check if the doubly linked list is empty. If it is empty, then there is
nothing to delete.
• If the list is not empty, then move to the last node of the doubly
linked list, say curr.
• Update the second-to-last node’s next pointer to NULL, curr->prev-
>next = NULL.
• Free the memory allocated for the node that was deleted.
Advantages of Doubly Linked List
• Efficient traversal in both directions: Doubly linked lists allow for
efficient traversal of the list in both directions, making it suitable for
applications where frequent insertions and deletions are required.
• Easy insertion and deletion of nodes: The presence of pointers to
both the previous and next nodes makes it easy to insert or delete
nodes from the list, without having to traverse the entire list.
• Can be used to implement a stack or queue: Doubly linked lists can
be used to implement both stacks and queues, which are common
data structures used in programming.
Disadvantages of Doubly Linked List
• More complex than singly linked lists: Doubly linked lists are more
complex than singly linked lists, as they require additional pointers for
each node.
• More memory overhead: Doubly linked lists require more memory
overhead than singly linked lists, as each node stores two pointers
instead of one.
Applications of Doubly Linked List
• Implementation of undo and redo functionality in text editors.
• Cache implementation where quick insertion and deletion of
elements are required.
• Browser history management to navigate back and forth between
visited pages.
• Music player applications to manage playlists and navigate through
songs efficiently.
• Implementing data structures like Deque (double-ended queue) for
efficient insertion and deletion at both ends.