# What Is a Doubly Linked List Data Structure?

//

Heather Bennett

A doubly linked list is a data structure that consists of a collection of nodes. Each node contains two pointers, one pointing to the previous node and another pointing to the next node in the sequence. This allows for efficient traversal in both directions.

Structure
A doubly linked list is similar to a singly linked list, with the addition of an extra pointer. Each node contains three fields: the data field, the pointer to the previous node (prev), and the pointer to the next node (next).

The main advantage of a doubly linked list over a singly linked list is that it allows for easy traversal in both directions. This can be useful in certain scenarios where backward traversal is required or when operations like deleting or inserting elements at arbitrary positions are frequent.

• Bidirectional Traversal: With both prev and next pointers available, we can traverse the list in either direction. This flexibility makes it easier to implement algorithms that require backward iteration.
• Efficient Insertion and Deletion: Inserting or deleting an element at an arbitrary position in a doubly linked list is more efficient compared to a singly linked list. In a singly linked list, we need to traverse from the beginning to find the previous node, whereas in a doubly linked list, we already have access to both prev and next pointers.
• Merging Lists: Merging two doubly linked lists is also straightforward by adjusting just a few pointers.

• Increased Memory Usage: Doubly linked lists require additional memory for storing the prev pointer. This may not be significant for small lists but can become significant for large lists with many nodes.
• Complexity: The presence of two pointers adds complexity to the implementation and maintenance of doubly linked lists. Care must be taken to ensure that pointers are correctly updated when performing operations like insertion, deletion, or rearrangement of nodes.

## Operations on Doubly Linked Lists

### Traversal

Traversing a doubly linked list can be done in both forward and backward directions. We can start from the head node and follow the next pointers to traverse forward or follow the prev pointers to traverse backward.

### Insertion

Inserting an element into a doubly linked list involves creating a new node and adjusting the pointers accordingly. There are different cases to consider depending on where the new node is being inserted:

• At the beginning: In this case, we update the next pointer of the new node to point to the current head, update the prev pointer of the current head to point to the new node, and finally update the head pointer to point to the new node.
• In between two nodes: Here, we adjust four pointers: next pointer of the previous node, prev pointer of the next node, prev pointer of the new node, and next pointer of the new node.
• At the end: Similar to inserting at the beginning, we update four pointers: next pointer of the previous last node, prev pointer of the new last node, next pointer of the new last node (which is null), and finally update tail pointer (if available) for easy access.

### Deletion

Deleting an element from a doubly linked list requires updating pointers based on different cases:

• Deleting the head node: We update the next pointer of the head’s next node to null and the prev pointer of the head’s next node to null. Then, we update the head pointer to point to the new head.
• Deleting an intermediate node: We adjust four pointers: next pointer of the previous node, prev pointer of the next node, prev pointer of the current node, and next pointer of the current node.
• Deleting the tail node: Similar to deleting at the beginning, we update four pointers: prev pointer of the last but one node, next pointer of the last but one node (which is null), prev pointer of the last node (which is null), and finally update tail pointer (if available) for easy access.

## Conclusion

Doubly linked lists provide a flexible data structure with bidirectional traversal capabilities. They offer advantages over singly linked lists in terms of efficient insertion/deletion and easier implementation for backward iteration. However, they come with increased memory usage and additional complexity in maintaining correct pointers.

By understanding how doubly linked lists work and their various operations, you can leverage them in your programs when bidirectional traversal is required or when frequent insertion/deletion operations need to be performed efficiently.