Doubly Linked List
, , , , , , , , , ,

Master Doubly Linked List in Data Structures (With Examples) 2025

Introduction to Doubly Linked List

When you’re learning data structures, you’ll often hear about Linked Lists. A Doubly Linked List (DLL) is a powerful and flexible variation of the basic Singly Linked List that allows you to traverse in both directions — forward and backward.

In this beginner-friendly article, we’ll explore:

  • What is a Doubly Linked List?
  • How it works (with diagrams)
  • Real-world uses
  • Advantages over singly linked list
  • Basic operations (insertion, deletion, traversal)
  • Code example in C/C++

🚀 Whether you’re preparing for coding interviews or improving your DSA skills, understanding doubly linked lists is a must!

What is a Doubly Linked List?

A Doubly Linked List is a type of linear data structure made up of nodes, where each node contains:

  • Data (the actual value)
  • Pointer to the next node
  • Pointer to the previous node

⚙️ Node Structure of Doubly Linked List:

+-------+----------+----------+
| prev  |   data   |   next   |
+-------+----------+----------+

Each node is connected to its previous and next node, unlike a singly linked list, which only has a next pointer.

Visual Representation of Doubly Linked List

Let’s say we have 3 nodes with data: 10, 20, and 30.

NULL <- [10] <-> [20] <-> [30] -> NULL

Here:

  • Node 10’s prev is NULL
  • Node 20’s prev points to 10 and next to 30
  • Node 30’s next is NULL

Basic Operations on Doubly Linked List

1. Insertion

  • At the beginning
  • At the end
  • At a specific position

2. Deletion

  • From the beginning
  • From the end
  • Specific node

3. Traversal

  • Forward traversal (left to right)
  • Backward traversal (right to left)

C Code Example: Basic DLL Operations

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    int data;
    struct Node* prev;
    struct Node* next;
} Node;

Node* head = NULL;

// Insert at end
void insertEnd(int value) {
    Node* newNode = (Node*)malloc(sizeof(Node));
    newNode->data = value;
    newNode->next = NULL;

    if (head == NULL) {
        newNode->prev = NULL;
        head = newNode;
        return;
    }

    Node* temp = head;
    while (temp->next != NULL)
        temp = temp->next;

    temp->next = newNode;
    newNode->prev = temp;
}

// Display forward
void displayForward() {
    Node* temp = head;
    printf("DLL (forward): ");
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->next;
    }
    printf("NULL\n");
}

// Display backward
void displayBackward() {
    Node* temp = head;
    if (!temp) return;
    
    // Go to last node
    while (temp->next != NULL)
        temp = temp->next;

    printf("DLL (backward): ");
    while (temp != NULL) {
        printf("%d <-> ", temp->data);
        temp = temp->prev;
    }
    printf("NULL\n");
}

int main() {
    insertEnd(10);
    insertEnd(20);
    insertEnd(30);
    
    displayForward();
    displayBackward();
    return 0;
}

Advantages of Doubly Linked List

  • Two-way traversal: You can navigate forward and backward.
  • Easier deletion: You can delete a node without needing the previous node pointer.
  • More flexible: Especially useful in complex data structures like Deques, Undo/Redo systems, Music Playlists, etc.

Disadvantages of Doubly Linked List

  • More memory: Each node requires extra space for the prev pointer.
  • More complex: Handling pointers can be error-prone for beginners.

Real-Life Applications of Doubly Linked List

  • Web browsers (backward and forward navigation)
  • Music playlist apps
  • Text editors (undo/redo operations)
  • Task managers (circular doubly linked lists for round-robin)

Conclusion DLL

Doubly Linked List
Master Doubly Linked List in Data Structures (With Examples) 2025

The Doubly Linked List is an essential topic in Data Structures and Algorithms (DSA). It offers a better way to manage dynamic data, especially when two-way traversal is needed. Though slightly more complex than a singly linked list, the benefits are powerful.

Whether you’re preparing for a coding interview, building a system-level application, or just learning C programming, mastering DLLs will give you a strong foundation in memory management and pointer logic.

FAQs of Doubly Linked List

Q1: Is doubly linked list faster than singly linked list?
Ans: DLL allows backward traversal and easier deletion, but it uses more memory. It’s more flexible, not necessarily faster.

Q2: Where is DLL used in real life?
Ans: Browser history, playlist navigation, undo/redo features, and memory management systems.

Q3: What is the main drawback of doubly linked list?
Ans: It uses extra memory due to the additional prev pointer and increases code complexity.

Q4: How is a doubly linked list different from a circular doubly linked list?
Ans: In a doubly linked list, the last node’s next pointer is NULL, whereas in a circular doubly linked list, the last node’s next points back to the head, forming a circle.

Q5: Can a doubly linked list be empty?
Ans: Yes, initially a doubly linked list can be empty with head pointing to NULL.

Q6: How do you insert a node at the beginning of a doubly linked list?
Ans: Create a new node, point its next to the current head, set the head’s prev to the new node, and then update head to the new node.

Q7: What happens when you delete the last node in a doubly linked list?
Ans: The second last node’s next pointer is set to NULL, and the last node is freed from memory.

Q8: Is it possible to implement a stack using a doubly linked list?
Ans: Yes, a doubly linked list can be used to implement a stack with push and pop operations.

Q9: How does traversal in a doubly linked list work?
Ans: You can traverse forward from head to NULL using next pointers or backward from the last node to head using prev pointers.

Q10: What are the memory requirements for a doubly linked list node?
Ans: Each node stores data plus two pointers (prev and next), so it uses more memory compared to a singly linked list node, which stores only one pointer.

Q11: Can doubly linked lists have cycles? How to detect them?
Ans: Yes, cycles can exist if nodes point back to earlier nodes. Cycle detection algorithms like Floyd’s Cycle-Finding Algorithm (tortoise and hare) can detect cycles.

Q12: Are doubly linked lists used in databases?
Ans: Yes, doubly linked lists are used to implement certain data structures like LRU caches, which are common in databases and operating systems.

Q13: How do you reverse a doubly linked list?
Ans: Swap the prev and next pointers for each node and update the head to the last node.

Leave a Reply

Your email address will not be published. Required fields are marked *