VOOZH about

URL: https://www.geeksforgeeks.org/cpp/doubly-linked-list-in-cpp/

⇱ Doubly Linked List in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Doubly Linked List in C++

Last Updated : 14 Oct, 2025

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.

👁 Advantages_-Disadvantages_-and-uses-of-Doubly-Linked-List

A doubly linked list is represented using nodes that have three fields:

  1. Data
  2. A pointer to the next node (next)
  3. A pointer to the previous node (prev)
👁 22

Implementation of Doubly Linked List in C++

A doubly linked list (DLL) can be implemented in C++ in two ways:

  1. Using STL std::list
  2. 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


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.


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

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.


Comment
Article Tags:
Article Tags: