Doubly Linked List Intro

A Doubly Linked List is a type of linked list in which each node contains three components:

  1. Data: The actual value or information stored in the node.
  2. Next: A pointer or reference to the next node in the sequence.
  3. Previous: A pointer or reference to the previous node in the sequence.

Key characteristics of a Doubly Linked List:

  1. Bidirectional traversal: Unlike a singly linked list, a doubly linked list can be traversed in both forward and backward directions.
  2. Each node has two links: This allows for more flexibility in list manipulation but requires more memory per node.
  3. The first node's previous pointer and the last node's next pointer both point to null.
  4. Easier deletion: Deletion of a node is more efficient as we have direct access to the previous node.
  5. More complex implementation: Due to the additional previous pointer, the implementation is slightly more complex than a singly linked list.

Advantages of Doubly Linked List:

  • Can be traversed in both directions
  • Deletion of a node is more efficient
  • Can quickly insert a new node before a given node

Disadvantages:

  • Requires more memory due to the extra previous pointer
  • Operations are slightly more complex due to handling two pointers

Real-world applications of Doubly Linked List:

  1. Implementation of navigation systems (forward/backward navigation)
  2. Music players with previous and next song functionality
  3. Undo/Redo operations in various applications
  4. Browser cache for storing recently visited pages
  5. Implementation of MRU (Most Recently Used) cache
Doubly Linked List Key Operations

Key operations on a Doubly Linked List include:

  1. Insertion (at the beginning, end, and at a specific position)
  2. Deletion (from the beginning, end, and at a specific position)
  3. Forward traversal
  4. Backward traversal
  5. Searching for an element

This is General Representation Of Doubly Linked List. Each node has two links: This allows for more flexibility in list manipulation but requires more memory per node.

The first node's previous pointer and the last node's next pointer both point to null.


Insertion In DLL: Add a node at the beginning, end, or a specific position.

First we write the Pseudocode for Doubly Linked List Operations

  • Add a node at the beginning. 
  • Adding a node at the end.
  • Adding a node at a specific position.

Node Structure

Define Node Structure:
    Node:
        - data: Integer
        - next: Node
        - prev: Node

Initialize Head and Tail Pointers

Initialize:
    - head = null
    - tail = null
Add Node at Last
Pseudocode

Add Node at First
Pseudocode

Insert Node at Specific Index
Pseudocode

Print Doubly Linked List Forward
Pseudocode

Print Doubly Linked List Reverse
Pseudocode

Code for Insertions In Doubly Linked List

Code for Insertions In Doubly Linked List

  • Add a node at the beginning. 
  • Adding a node at the end.
  • Adding a node at a specific position.
Insertions Operations In Doubly Linked List

Code Output
Forward Direction
14->5->12->200->4->10->20->null
Reverse Direction
20->10->4->200->12->5->14->null