Doubly Linked List in C++
A Doubly Linked List (DLL) is a type of linked list in which each node contains three parts: the data, a pointer to the next node, and a pointer to the previous node. This structure allows traversal of the list in both forward and backward directions, unlike a singly linked list which can only be traversed forward.

A doubly linked list is represented using nodes that have three fields:
- Data
- A pointer to the next node (next)
- A pointer to the previous node (prev)

Implementation of Doubly Linked List in C++
A doubly linked list (DLL) can be implemented in C++ in two ways:
- Using STL
std::list - Manual Implementation Using Pointers
1. Using STL std::list
C++ std::list implements a doubly linked list, where each node has pointers to the next and previous nodes. It supports efficient bidirectional traversal and O(1) insertions/deletions, while handling memory management internally.
- Automatically grows/shrinks and manages memory internally.
- Supports forward and backward iteration using iterators.
Different Types of 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
2. Manual Implementation Using Pointers
A doubly linked list can be implemented manually by creating a node with data, next, and prev pointers. You handle memory allocation, linking, insertion, deletion, and traversal yourself, which gives full control but is more error-prone than using STL.
#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:
Create the head node.
- Allocate a node and set head to it. Its prev and next should be null/None.
Create the next node and link it to head.
- head.next = new Node(value2)
- head.next.prev = head
Create further nodes the same way.
- For the third node:
=> head.next.next = new Node(value3)
=> head.next.next.prev = head.next - Repeat until you have the required nodes.
Ensure the tail's next is null.
The last node you created must have next == null
Set / keep track of head (and optionally tail).
Use head to access the list from the front. Keeping a tail pointer simplifies appends.
Basic Operations on C++ Doubly Linked List
- Traversal : Display Linked List Elements
- Insertion : At the Beginning, At the End and At the specific position
- Deletion : From the Beginning, From End and From a Specific Position
Application of Doubly Linked List
Advantages of Doubly Linked List
- 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.
- Useful for implementing undo/redo, browser history, and playlist navigation.
Disadvantages of Doubly Linked List
- 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.
- Less cache-friendly as nodes are scattered in memory.