Doubly Linked List
Doubly linked list is a linear data structure. It is a collection of objects defined as nodes. But unlike a linked list, the node has two pointers, one is the previous pointer and the other is the next pointer. Just like the linked list nodes are stored in random locations in the memory instead of being stored in consecutive locations.
Doubly Linked List vs Linked List
-
Doubly linked lists allow us to traverse the linked list in both forward and backward directions, but this comes
prev
at the cost of extra space required for pointers to each node. - Inserting an element in a doubly linked list is very easy as we do not have to maintain multiple pointers or traverse the linked list to find the previous pointer, but we have to modify more pointers compared to a linked list.
Doubly Linked List Traversal Algorithm
The way forward
Let head
be the first node of the linked list.
-
Initializes a node
curr
pointing to a linked listhead
. -
While
curr
has not yet reached the end of the list, i.e.curr
! =NULL
, do the following:- Prints the data stored in the current node.
-
curr=curr->next
; -
If it is the last node, it is stored
tail
as to facilitate backward traversal.
Likewise, we can traverse backwards by tail
starting at and curr
changing to .curr->prev
Reverse direction
Let tail
be the last node of the linked list.
-
Initializes a node
curr
pointing to a linked listtail
. -
While
curr
has not yet reached the head of the list, i.e.curr
!=NULL
, do the following:- Prints the data stored in the current node.
-
curr = curr->上一个
Doubly Linked List Insertion
4
Doubly linked lists have situations
when inserting elements , just like linked lists.
Insert the node at push()
the beginning of the DLL
- Create a new node for
with data
x
and .prev
NULL
temp
-
Set
temp->next
tohead
andhead->prev
set to to inserttemp
before .head
temp
-
Sets
temp
to the beginning of the linked list.
Append()
Insert a node at the end of the
DLL
-
Create a new node
temp
whose data isx
and whoseprev
isNULL
. -
Initializes pointer
head
totail
. -
If the linked list is empty,
temp
sets to the linked list'shead
and then returns . -
Otherwise, iterate to the end of the linked list, making
tail->next
!=NULL
so that you reach the last element -
Set
tail->next
totemp
, andtemp->prev
set totail
.
insertBefore()
Inserts a node before
the given node
-
If
next
==NULL
, then return; x
Create a new node with datacurr
.-
Set
curr->prev
to to add a new node before , andnext->prev
set to to establish a backward link.next
next->prev
curr
-
Set
curr->next
tonext
, and finally checkcurr->prev
whether isNULL
. -
If not
NULL
,curr->prev->next
set to tocurr
complete the insertion, otherwisecurr
is the first node of the linked list. Sethead
tocurr
.
insertAfter()
Inserts a node after
the given node
-
If
prev
==NULL
, then return; x
Create a new node with datacurr
.-
Set
curr->next
to to add a new node after , andprev->next
set to to establish a forward link.prev
prev->next
curr
-
Set
curr->prev
toprev
, and finally checkcurr->next
if is NULL. If notNULL
,curr->next->prev
set tocurr
to complete the insert.
Doubly Linked List Insertion Process
-
Insert the node at
push()
the beginning of the DLL Append()
Insert a node at the end of the DLLinsertBefore()
Inserts a node before the given nodeinsertAfter()
Insert a node after the given node
Doubly linked list traversal and insertion implementation
#include <bits/stdc++.h>
using namespace std;
class Node {
public:
int data;
Node* next;
Node* prev;
};
void push(Node** head, int x)
{
Node* curr = new Node();
curr->data = x;
curr->next = (*head);
curr->prev = NULL;
if ((*head) != NULL)
(*head)->prev = curr;
(*head) = curr;
}
void insertAfter(Node* prev, int x)
{
if (prev == NULL)
{
cout << "the given previous node cannot be NULL";
return;
}
Node* curr = new Node();
curr->data = x;
curr->next = prev->next;
prev->next = curr;
curr->prev = prev;
if (curr->next != NULL)
curr->next->prev = curr;
}
void insertBefore(struct Node** head, struct Node* next, int x)
{ if (next == NULL) {
return;
}
Node* curr = new Node();
curr->data = x;
curr->prev = next->prev;
next->prev = curr;
curr->next = next;
if (curr->prev != NULL)
curr->prev->next = curr;
else
(*head) = curr;
}
void append(Node** head, int x)
{
Node* curr = new Node();
Node* tail = *head;
curr->data = x;
curr->next = NULL;
if (*head == NULL)
{
curr->prev = NULL;
*head = curr;
return;
}
while (tail->next != NULL)
tail = tail->next;
tail->next = curr;
curr->prev = tail;
return;
}
void printList(Node* node)
{
Node* tail = NULL;
cout << "Forward Traversal:";
while (node != NULL)
{
cout << " " << node->data << " ";
tail = node;
node = node->next;
}
cout << "\nReverse Traversal:";
while (tail != NULL)
{
cout << " " << tail->data << " ";
tail = tail->prev;
}
cout << "\n";
}
int main()
{
Node* head = NULL;
append(&head, 6);
push(&head, 7);
push(&head, 1);
append(&head, 4);
printList(head);
insertAfter(head->next, 8);
insertBefore(&head, head->next->next, 3);
printList(head);
return 0;
}
Complexity of Doubly Linked List Traversal and Insertion Algorithms
Traversal
- Average situation
To traverse the complete doubly linked list, we have to visit every node. Therefore, if it has n
nodes, the average case time complexity of traversal is about O(n)
. The time complexity is about O(n)
.
- Best Case
The time complexity of the best case is O(n)
. It is the same as the time complexity of the average case.
- Worst case scenario
The worst case time complexity is O(n)
. It is the same as the best case time complexity.
The space complexity of the traversal algorithm is O(1)
, since curr
no space is required other than the pointer.
Insertion method
- Average situation
In all 4
cases inserting an element requires at most 4
link changes, so the time complexity of insertion is O(1)
.
- Best Case
The time complexity of the best case is O(1)
. It is the same as the time complexity of the average case.
- Worst case scenario
The worst case time complexity is O(1)
. It is the same as the best case time complexity.
For all 4
insertion methods, the space complexity of the insertion algorithm is O(1)
.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Convert a binary tree to a binary search tree
Publish Date:2025/03/18 Views:52 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
Binary Search Tree
Publish Date:2025/03/18 Views:181 Category:ALGORITHM
-
A binary search tree (BST) is an ordered binary tree data structure based on nodes. A node has a value and two child nodes (a binary tree has at most two child nodes) connected to each other. A node has a value and two child nodes (a binary
Binary Tree Traversal
Publish Date:2025/03/18 Views:115 Category:ALGORITHM
-
A binary tree is a non-linear data structure. It is called a binary tree because each node has at most two children. These children are called left and right children. It can also be interpreted as an undirected graph where the topmost node
Circular Doubly Linked List
Publish Date:2025/03/18 Views:67 Category:ALGORITHM
-
A circular doubly linked list is a combination of a circular linked list and a doubly linked list. Its two nodes are connected by a previous and next pointer. The next pointer of the last node points to the first node, and the previous poin
Circular Linked List
Publish Date:2025/03/18 Views:172 Category:ALGORITHM
-
A circular linked list is a data structure that is slightly more complex than a linked list. It is a linked list where all the nodes are connected in a loop and form a chain. The last node does not have a NULL . Instead, it stores the addre
Linked List Merge Sort
Publish Date:2025/03/18 Views:138 Category:ALGORITHM
-
In this article, we will learn how to sort a linked list using merge sort algorithm. It is one of the most preferred algorithms to sort a linked list because slow pointer random access makes the performance of other algorithms worse (for ex
Detailed introduction to the implementation principle of linked lists
Publish Date:2025/03/18 Views:98 Category:ALGORITHM
-
A linked list is a linear data structure. It is a collection of objects defined as nodes. But in a linked list, the nodes are stored in random locations in the memory and not in consecutive locations. A node in a linked list consists of: Da
Java 中的 Trie 数据结构
Publish Date:2023/08/05 Views:129 Category:Java
-
本文介绍了 Java 中的 Trie 数据结构。Java 中的 Trie 数据结构 Trie 词是从单词 Retrieval 中提取出来的,它是一种用于存储字符串集合的排序数据结构。
将二叉树转换为二叉搜索树
Publish Date:2023/03/20 Views:187 Category:算法
-
本教程教大家如何将二叉树转换为二叉搜索树。二叉树是一种非线性数据结构。它被称为二叉树,因为每个节点最多有两个子节点。这些子节点被称为左子和右子。