![]() |
VOOZH | about |
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:
A doubly linked list (DLL) can be implemented in C++ in two ways:
std::liststd::listC++ 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.
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.Forward: 5 <-> 15 <-> 10 <-> 20 <-> NULL Backward: 20 <-> 10 <-> 15 <-> 5 <-> NULL After deletion: 15 <-> 10 <-> NULL
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.
10 <-> 20 <-> 30 <-> 40
Explanation:
Create the head node.
Create the next node and link it to head.
Create further nodes the same way.
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.
prev).