Circular Doubly Linked List
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 pointer of the first node points to the last node. It can be traversed in both directions, either by jumping from the tail to the head or from the head to the tail. It is also used to implement advanced data structures such as Fibonacci heaps.
Circular doubly linked list traversal
We simply check the condition that the next node in the iteration is not head
, and then traverse the circular doubly linked list, and then move out of the iteration temp
from temp->next
or depending on forward/backward temp->prev
.
Positive
Let head
be the first node of the linked list.
-
Initializes a pointer
head
to a node in a linked listcurr
. -
While
curr
the end of the list has not been reached, i.e.curr->next
! =head
, do this:- Prints the data stored in the current node.
-
curr=curr->next
;
Likewise, we can traverse backwards by tail
starting at and curr
changing to .curr->prev
Reverse
Let tail
(i.e. head->prev
) 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->prev
!=tail
, do this:- Prints the data stored in the current node.
-
curr = curr->上一个
Insertion into a circular doubly linked list
There are 3
different insertion situations.
Insert an element at the beginning of a circular doubly linked list
-
Create a node with the given data
temp
. -
Set
temp->next
tohead
andtemp->prev
set totail
to insert betweenhead
and .tail
temp
-
Set
tail->next
andhead->prev
totemp
to complete the insertion. -
Set
head
totemp
to move head to the newly inserted element.
Insert an element at the end of a circular doubly linked list
-
Create a node with the given data
temp
. -
If the list is empty, creates a node with the given data
temp
, points bothtemp->prev
andtemp->next
to itself, and sets it tohead
and returns it. - Execute the steps you want to insert at the beginning, except the last step.
Inserting an element in the middle of a circular doubly linked list
Let X
be the value of the node after which the element is to be inserted.
-
Create a node with the given data
temp
. -
Initializes
head
a pointer tocurr
. -
Iterate until you find a node with
数据
=X
. Store its next node as下一个
. -
Set
curr->next
totemp
. -
Set
temp->prev
tocurr
, andtemp->next
set tonext
. -
Finally,
下一个->上一个
set it to温度
.
Insertion process of circular bidirectional linked list
- Insert an element at the beginning of a circular doubly linked list
- Insert an element at the end of a circular doubly linked list
- Inserting an element in the middle of a circular doubly linked list
Circular bidirectional linked list traversal and insertion implementation
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *next;
Node *prev;
};
void insertEnd(Node** head, int value)
{
if (*head == NULL)
{
Node* temp = new Node;
temp->data = value;
temp->next = temp->prev = temp;
*head = temp;
return;
}
Node *tail = (*head)->prev;
Node* temp = new Node;
temp->data = value;
temp->next = *head;
(*head)->prev = temp;
temp->prev = tail;
tail->next = temp;
}
void insertBegin(Node** head, int value)
{
Node *tail = (*head)->prev;
Node* temp = new Node;
temp->data = value;
temp->next = *head;
temp->prev = tail;
tail->next = (*head)->prev = temp;
*head = temp;
}
void insertAfter(Node** head, int value1,
int value2)
{
Node* temp = new Node;
temp->data = value1;
Node *curr = *head;
while (curr->data != value2)
curr = curr->next;
Node *next = curr->next;
curr->next = temp;
temp->prev = curr;
temp->next = next;
next->prev = temp;
}
void printList(Node* head)
{
Node *curr = head;
printf("Forward Traversal:");
while (curr->next != head)
{
printf("%d ", curr->data);
curr = curr->next;
}
printf("%d ", curr->data);
printf("\nBackward Traversal:");
Node *tail = head->prev;
curr = tail;
while (curr->prev != tail)
{
printf("%d ", curr->data);
curr = curr->prev;
}
printf("%d ", curr->data);
cout << "\n";
}
int main()
{
Node* head = NULL;
insertEnd(&head, 2);
insertBegin(&head, 1);
insertEnd(&head, 4);
printList(head);
insertEnd(&head, 5);
insertAfter(&head, 3, 2);
printList(head);
return 0;
}
Complexity of traversal and insertion algorithms for circular doubly linked lists
Traversal
- Average situation
To traverse the complete doubly linked list, we have to visit every node. If we have n
nodes, the average case time complexity of the 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)
, because curr
no additional space is required except for the pointer.
insert
- Average situation
To insert the element we want to insert at tail
and , we need to change at most links, so the time complexity is . But to insert between the two, we may have to move nodes, so the time complexity is .head
4
O(1)
n-1
O(n)
- Best Case
For all 3
cases, the best case time complexity is O(1)
.
- Worst case scenario
For the first two cases, the worst case time complexity is O(1)
, and for the third case O(n)
, it is the same as the average case time complexity.
For all 3 types of insertion, 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 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
Doubly Linked List
Publish Date:2025/03/18 Views:69 Category:ALGORITHM
-
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 nod
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:算法
-
本教程教大家如何将二叉树转换为二叉搜索树。二叉树是一种非线性数据结构。它被称为二叉树,因为每个节点最多有两个子节点。这些子节点被称为左子和右子。