A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the previous node and a pointer to the next node. This allows for quick and easy insertion and deletion of nodes from the list, as well as efficient traversal of the list in both directions.
Each node in a Doubly Linked List contains the data it holds, a pointer to the next node in the list, and a pointer to the previous node in the list. By linking these nodes together through the next and prev pointers, we can traverse the list in both directions (forward and backward), which is a key feature of a Doubly Linked List.
Creating a Doubly Linked List with 4 Nodes
Create the head node.
Allocate a node and set head to it. Its prev and next should be null/None.
Create the next node and link it to head.
head.next = new Node(value2)
head.next.prev = head
Create further nodes the same way.
For the third node: => head.next.next = new Node(value3) => head.next.next.prev = head.next
Repeat until you have the required nodes.
Ensure the tail's next is null. The last node you created must have next == null
Set / keep track of head (and optionally tail). Use head to access the list from the front. Keeping a tail pointer simplifies appends.
Bidirectional Traversal - You can traverse forward (using next) as well as backward (using prev).
Efficient Deletion - Given a pointer to a node, you can delete it in O(1) time (no need to traverse from the head), since you can update both prev and next.
Insertion at Both Ends - Insertion at head or tail is efficient because you can update both directions easily.
Easy to Implement Deque / Navigation Features - Useful for undo/redo, browser history, and music playlist navigation, where both forward and backward movement is needed.
Disadvantages of Doubly Linked List
Extra Memory Per Node - Each node requires an additional pointer (prev), making DLL more memory-consuming than singly linked list.
More Complex Implementation - Both prev and next must be handled carefully during insertion and deletion, which increases chances of errors (broken links, null pointer issues)
Slower Operations Due to Overhead - Extra pointer manipulations during insertion/deletion cause slightly more overhead compared to singly linked list.
Not Cache-Friendly - Like singly linked list, nodes are scattered in memory, so traversals may be slower compared to arrays due to poor locality of reference.