Doubly linked list in C
A doubly linked list differs from a singly linked list because each node contains an additional pointer that points the previous node, along with the next pointer and data, similar to a singly linked list.
Singly vs Doubly Node Structure
A singly linked list node has:
- Data: The value stored in the node.
- Next pointer: A pointer to the next node in the list.
struct Node { int data; // Data stored in the node struct Node *next; // Pointer to the next node } |
A doubly linked list node has:
- Data: The value stored in the node.
- Next pointer: A pointer to the next node.
- Prev pointer: A pointer to the previous node.
struct Node { int data; // Data stored in the node struct Node *next; // Pointer to the next node struct Node *prev; // Pointer to the previous node } |
The descriptive diagram is given below
Singly vs Doubly linked list Example:
Due to previous pointer , we got ability to move backward in doubly linked list which was absent in singly linked list, as shown in following figure
Singly vs Doubly linked list Comparison
Here’s a detailed comparison between Singly Linked List (SLL) and Doubly Linked List (DLL):
Feature | Singly Linked List (SLL) | Doubly Linked List (DLL) |
---|---|---|
Node Structure | Contains data and a pointer to the next node. | Contains data, a pointer to the next node, and a pointer to the previous node. |
Memory Usage | Requires less memory (1 pointer per node). | Requires more memory (2 pointers per node). |
Traversal | Forward traversal only. | Allows forward and backward traversal. |
Insertion | Easy at the beginning or end, more complex in the middle (requires traversal). | Easier at any position due to bidirectional links. |
Deletion | Needs traversal to find the previous node for deletion. | Direct access to the previous node simplifies deletion. |
Complexity | Simpler to implement. | More complex due to managing two pointers per node. |
Reverse Traversal | Not possible. | Possible. |
Use Cases | – Stacks, simple queues. | – Double-ended queues (deques), navigation systems. |
Performance (Insertion/Deletion) | Slower when operating in the middle of the list. | Faster for middle operations due to access to both neighbors. |
Flexibility | Less flexible in operations. | More flexible for complex operations. |
Preferred When | Memory is limited, and simple operations are needed. | Performance and bidirectional access are critical. |