Doubly Linked List in C++: Concepts + Code Example
Introduction
A doubly linked list is an advanced form of linked list in C++ that allows traversal in both forward and backward directions. Unlike a singly linked list, each node in a doubly linked list contains three parts: the data, a pointer to the next node, and a pointer to the previous node. This two-way linking makes operations like insertion, deletion, and reverse traversal more efficient and flexible. Doubly linked lists are commonly used in applications such as navigation systems, undo-redo operations, and memory management. In this blog post, we’ll explore how doubly linked lists work in C++ and why they are an important data structure to understand.
Let’s get started with Doubly Linked Lists.
Today, we going to use Doubly Linked List in C++. A doubly linked list (DLL) is a type of linked list where each node contains data and two pointers: one pointing to the next node and another to the previous node. This structure enables traversal in both forward and backward directions, unlike a singly linked list.
A doubly linked list is a data structure made of connected nodes. Each node contains data along with two pointers: one that points to the next node in the list and another that points to the previous node.
This two-way connection allows the list to be traversed both forward and backward. Because each node keeps track of its neighbors, inserting and deleting nodes is easier and more flexible than in a singly linked list. This is an example code of doubly linked list:
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
Node* prev;
};
int main() {
Node* first = new Node{10, nullptr, nullptr};
Node* second = new Node{20, nullptr, first};
first->next = second;
cout << first->data << " <-> " << second->data;
return 0;
}
This code creates two nodes where each node points to the next and previous node, forming a doubly linked list.
Now, Here's the overall code to understand all these statements better:
As shown in the above picture, this code demonstrates a simple doubly linked list using two nodes. Each node contains an integer value and two pointers: next and prev. The head node stores the value 5 and points forward to the tail node, while the tail node stores the value 15 and points back to the head. By linking the nodes this way, the program can display the list in both forward and backward directions in the console, showing how a doubly linked list allows two-way traversal.
Important Notes (Things Beginners Often Miss with Doubly Linked Lists in C++)
When learning doubly linked lists, beginners often underestimate the importance of maintaining both pointers correctly. Every insertion or deletion must update two links—next and prev. Forgetting to update either one can break the list or cause undefined behavior. Always think in terms of two-way connections when modifying nodes.
Another common mistake is mishandling edge cases, such as inserting into an empty list, deleting the first (head) node, or deleting the last (tail) node. In these cases, prev or next pointers can be nullptr, and failing to check for this can lead to segmentation faults. Proper null checks are essential.
Beginners also assume that doubly linked lists are always better than singly linked lists. While they allow backward traversal and easier deletions, they consume more memory because of the extra pointer in each node. Choosing a doubly linked list should depend on whether two-way traversal or frequent deletions are truly required.
Memory management is another area often ignored. Nodes created using new must be deleted properly to avoid memory leaks. Since nodes are interconnected, deleting one node requires careful pointer adjustment before freeing memory.
Finally, many learners forget that traversal should use a temporary pointer, not the head itself. Moving the head pointer during traversal can cause the list to be lost. Understanding doubly linked lists also makes it easier to learn circular doubly linked lists and other advanced data structures later. Practicing pointer diagrams and step-by-step tracing will greatly improve accuracy and confidence when working with doubly linked lists in C++.
Conclusion
Doubly linked lists offer greater flexibility compared to singly linked lists by allowing movement in both directions. This makes them especially useful for applications that require frequent insertions, deletions, or backward traversal. While they use extra memory due to additional pointers, the advantages they provide often outweigh the cost in many real-world scenarios. Understanding doubly linked lists strengthens your grasp of pointers and dynamic data structures, preparing you for more advanced topics in C++ and data structure design.
Comments
Post a Comment