A Doubly Linked List (DLL) is a linear data structure in which each node stores data along with pointers to both the next and previous nodes. Unlike a singly linked list, it supports traversal in both forward and backward directions.
- Each node contains data, a next pointer, and a previous pointer.
- Supports bidirectional traversal and efficient insertion/deletion.
- Widely used in browser history, undo/redo, and navigation systems.

Structure of a Doubly Linked List
A doubly linked list consists of nodes connected using two pointers. Each node contains:
- Data -> Stores the value.
- Next Pointer (next) -> Points to the next node.
- Previous Pointer (prev) -> Points to the previous node.

Implementation of Doubly Linked List
A doubly linked list can be implemented in C++ in two different ways.
Using STL std::list
The C++ Standard Library provides the std::list container, which internally implements a doubly linked list.
- Automatically manages memory.
- Supports efficient insertion and deletion.
- Allows forward and backward traversal using iterators.
Common Operations
- push_back(value) adds a node at the end of the list.
- push_front(value) adds a node at the beginning of the list.
- pop_back() removes the last node efficiently.
- pop_front() removes the first node efficiently.
- insert(iterator, value) inserts a node at a specific position in the list.
#include <iostream>
#include <list>
using namespace std;
int main()
{
// Create a doubly linked list using STL
list<int> dll;
// Insert elements at the end
dll.push_back(10);
dll.push_back(20);
// Insert element at the beginning
dll.push_front(5);
// Insert element at a specific position (after first element)
auto it = dll.begin();
++it; // move iterator to second position
dll.insert(it, 15);
// Forward traversal of the list
cout << "Forward: ";
for (int val : dll)
cout << val << " <-> ";
cout << "NULL" << endl;
// Backward traversal of the list
cout << "Backward: ";
for (auto rit = dll.rbegin(); rit != dll.rend(); ++rit)
cout << *rit << " <-> ";
cout << "NULL" << endl;
// Remove element from the beginning
dll.pop_front();
// Remove element from the end
dll.pop_back();
// Forward traversal after deletions
cout << "After deletion: ";
for (int val : dll)
cout << val << " <-> ";
cout << "NULL" << endl;
return 0;
}
Output
Forward: 5 <-> 15 <-> 10 <-> 20 <-> NULL Backward: 20 <-> 10 <-> 15 <-> 5 <-> NULL After deletion: 15 <-> 10 <-> NULL
Manual Implementation Using Pointers
A doubly linked list can also be implemented by creating nodes that explicitly store pointers to both neighboring nodes.
Steps
- Create a node containing data, next, and prev pointers.
- Allocate the head node.
- Link each newly created node with both its previous and next nodes.
- Keep the last node's next pointer as nullptr.
- Traverse the list using the next pointer (or prev pointer for reverse traversal).
#include <iostream>
using namespace std;
class Node
{
public:
int data;
Node *prev;
Node *next;
Node(int value)
{
data = value;
prev = nullptr;
next = nullptr;
}
};
int main()
{
// Create the first node (head of the list)
Node *head = new Node(10);
// Create and link the second node
head->next = new Node(20);
head->next->prev = head;
// Create and link the third node
head->next->next = new Node(30);
head->next->next->prev = head->next;
// Create and link the fourth node
head->next->next->next = new Node(40);
head->next->next->next->prev = head->next->next;
// Traverse the list forward and print elements
Node *temp = head;
while (temp != nullptr)
{
cout << temp->data;
if (temp->next != nullptr)
{
cout << " <-> ";
}
temp = temp->next;
}
return 0;
}
Output
10 <-> 20 <-> 30 <-> 40
Explanation
- The first node is created as the head of the list.
- Each new node is linked with both its previous and next nodes.
- Because every node stores prev and next pointers, the list can be traversed in both directions.
Application of Doubly Linked List
Advantages of Doubly Linked List
A doubly linked list provides efficient navigation and modification of nodes.
- Allows traversal in both forward and backward directions.
- Enables deletion of a node in O(1) time if its pointer is known.
- Supports easy insertion at both the beginning and end.
Disadvantages of Doubly Linked List
Despite its flexibility, a doubly linked list has some drawbacks.
- Requires extra memory for storing an additional pointer (prev).
- Insertion and deletion are more complex due to handling of two pointers.
- Slightly slower operations because of extra pointer updates.