Circular N Doubly Linked List
Circular N Doubly Linked List
List
The list can be traversed in single direction, yet the from any node, we can reach to any other node in the list.
Any node can be a starting point. We can traverse the whole list by starting from any point. We just need to stop
when the first visited node is visited again.
Creating a circular linked list
struct node *new= (struct node*) malloc(sizeof(struct node));
new->data = value;
new->next = NULL; value NULL 10 NULL
new
1000
Head = new;
new->next = Head;
3
Add the Next Node to the List
new
Now the first node will point to new node
Head->next = new;
new->next = Head; Head 10 2050 20 NULL
1000
1000 2050
4
Add the Third Node to the List
In such case, we need to traverse the list to get the address of last node.
P
Insert the new node
5
Traversal of a circular linked list
Traverse the list and display the content. Start with the “Head” pointer and go through all the nodes until we
reach last node.
6
Insertion in the Circular Linked List
Inserting a node in the circular linked list such that its basic property is satisfied.
A new node can be inserted at any position in the list, however we are considering three distinct cases.
• Insertion at beginning
• Insertion at last
• Insertion after a specific node based on
Key
Index (or position) of node
Insert at first position
Insert P
80 NULL
new
In circular linked list, the insert operation at the beginning requires traversing the entire list, as we need to
change the next field of the last node.
new
Head = new; 80 3050
• New->next
• P->next
• Head
Insert at last position
New->next = Head;
P
Analysis of Insertion at last operation on the circular linked list
• New->next
• P->next
Insert at Specific position
Task 1: Insert a new node in the given list after the node containing key value as 30.
P
80 NULL
new->next = P->next
new
P->next = new
Task 2: Insert a new node in the given circular list after the 3rd node.
Task 3: Search a key in the circular list and return the position of node.
Deletion from the Circular Linked List
Deletion operation can also be performed at various locations in the list such as:
P ->next = Head->next;
P = Head;
Head 10 3150 20 3250 30 3350 40 3050
3050 3150 3250 3350
P
Head = Head->next;
10 3150 20 3250 30 3350 40 3050
3050 3150 3250
P
Head 3350
Int x = P->data;
P->next = NULL;
10 NULL 20 3250 30 3350 40 3050
3050 3150 3250 3350
P
Head
Free(P);
Return x;
20 3250 30 3350 40 3050
3150 3250 3350
Head
Analysis of Deletion from last operation on the circular linked list
• P->next
• Head
Task 1: Delete a node from the given list from last.
q->next = Head;
10 3150 20 3250 30 3350 40 3050
Head
3050 3150 3250 3350
q P
10 3150 20 3250 30 3350 40 NULL
P->next = NULL; Head
3050 3150 3250 3350
q P
Int x = P->data;
Free(P);
In doubly linked list, we can move in both directions. However, it also provides sequential access to the
elements.
We store two address fields in each node of the DLL, it requires more memory than singly linked list.
The insertion and deletion operation in the DLL is complex as they require to change more number of pointers.
Structure of a node of DLL
Struct node
{
Struct node *prev;
prev data next
int data;
Struct node * next;
}
new
new->prev = NULL;
new->data = value; NULL 10 NULL
new->next = NULL;
new
Head->next = new;
New->prev = Head 1040 2040
new
1040 2040
new
Add this node here
1040 2040 2150
P->next = new;
New->prev = P;
NULL 5 NULL
new
Head->prev = new;
New->next = Head;
Head->new;
NULL 5 3050
new 3020
Head->prev = new;
New->next = Head; Number of pointers needed: 0
Time complexity = O(1) Number of pointers to change: 3
Head->new; • Head->prev
• New->next
• Head
Insert the node somewhere in the middle
new
NULL 5 NULL
New->next = P->next;
Delete a node from the DLL, and release the memory occupied by that node.
NULL 10 NULL
temp->next = NULL temp
3050
Head 3050 20 3250 3150 30 3350 3250 40 NULL
Int x = temp->data;
3150 3250 3350
Free(temp);
Return x;
Task 2: Insert a new node in the given doubly list after the node containing key value as 30.
Task 3: Insert a new node in the given list after the 3rd node.
Task 4: Search a key in the doubly list and return the position of node.
Task 6: Traverse the doubly list in reverse direction and display the elements.