You are on page 1of 19

Doubly Circular LINKED LIST

• A Circular doubly linked list is one which has both the


successor and predecessor pointer pointed in circular manner
• The objective behind considering circular doubly linked list is
to simplify the insertion and deletion operations performed
on circular doubly linked list.
• In circular doubly linked list, a list is never kept empty which
can be accomplished by providing a special node called head
node head
prev 0 next prev 10 next
Insertion Operation on Circular Doubly
Linked list
• Inserting a node at the beginning of the list
• Inserting a node at the end of the list
• Inserting a node before or after specified
position
Inserting a node at the beginning of
the Cirular Doubly Linked List
1. a. Create a head node ‘head’ .
b. Set its left and right link point to head
head->next=head
head->prev=head
c. Set its info field to zero
head->info=0
2. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data
3. If list is empty i.e if(head->next==head)
a. set the next field of head point to newnode and set the prev field of
newnode point to head
head->next=pnew
pnew->prev=head
b. make the next field of new node point to head node and make the prev field of newnode point to head node
pnew->next=head
head->prev=pnew
4. Else
a. point the node immediately after the head node by ptemp
ptemp=head->next
b. set the next field of the head node point to the new node and prev field of newnode point to the
head node
head->next=pnew
pnew->prev=head
c. set the next field of new node point to the node pointed by ptemp and set the prev field of node
pointed by ptemp point to new node
pnew->next=ptemp
ptemp->prev=pnew
Inserting a node at the beginning of
Circular Doubly Linked List
pfirst

,plast=pnew
pnew pfirst ,plast

,plast

pnew pfirst

plast
Inserting a node at the end of the
Circular Doubly Linked List
• 1. a. Create a head node ‘head’ .
• b. Set its left and right link point to head
• head->next=head
• head->prev=head
• c. Set its info field to zero
• head->info=0
• 2. Create a new node “pnew” and assign the data in its info field “info”.
• pnew->info=data
3. If list is empty i.e if (head->next==head)
• a. set the next field of head point to newnode and set the prev field of
• newnode point to head
• head->next=pnew
• pnew->prev=head
• b. make the next field of new node point to head node and make the prev field of newnode point to head node
• pnew->next=head
• head->prev=pnew
4. Else
• a. point the last node by ptemp
• ptemp=head->prev
• b. set the prev field of the head node point to the new node and next field of newnode point to the
• head node
• head->prev=pnew
• pnew->next=head
• c. set the prev field of new node point to the node pointed by ptemp and set the next field of node
• pointed by ptemp point to new node
• pnew->prev=ptemp
• ptemp->next=pnew
Inserting a node at the end of the
Doubly Circular linked list
pfirst ,pthis
plast

pfirst pthis plast

plast
pfirst
Inserting a node before the specified
position in the list
1. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data

2. Input the specified location of a node “loc”


3. make the new pointer “pthis” point to the node immediately after the head node and move the pointer “pthis” until it points to the specified node

pthis=head->next
for(i=1;i<=loc-1;i++)
pthis=pthis->next

4. Store the address of node just before the specified node in temporary pointer “ptemp”
ptemp=pthis->prev

make the next field of the node pointed by “ptemp” point to newnode and make the prev field of new node
point to node pointed by ptemp
ptemp->next=pnew
pnew->prev=ptemp

Also make the next field of new node point to the node ,pointed by pthis. And make the prev field of node pointed by pthis point to new node
pnew->next=pthis
pthis->prev=pnew
Inserting a node before the specified position in the
Doubly Circular Linked list
pfirst
Node2 Node3 (specified node)
Node1 plast
Loc=3

pthis
plast
pfirst pthis
ptemp

plast

pfirst pthis
Inserting a node after the specified
position in the Doubly Circular Linked list
1. Create a new node “pnew” and assign the data in its info field “info”.
pnew->info=data

2. Input the specified location of a node “loc”


3. . make the new pointer “pthis” point to the node immediately after the head node and move the pointer “pthis” until it points to the specified node
pthis=head->next
for(i=1;i<=loc-1;i++)
pthis=pthis->next

4. Store the address of node just after the specified node in temporary pointer “ptemp”
ptemp=pthis->next

make the prev field of the node pointed by “ptemp” point to newnode and make the next field of newnode point to the node pointed by ptemp
point to node pointed by ptemp
ptemp->prev=pnew
pnew->next=ptemp

Also make the prev field of new node point to the node ,pointed by pthis. And make the next field of node pointed by pthis point to newnode
pnew->prev=pthis
pthis->next=pnew
Inserting a node after the specified
position in the Doubly Circular Linked
pfirst 2 listNode3 22
Node2 (specified node)
Node1 plast

Loc=2

ptemp
pthis ptempplast
pfirst pthis
ptemp

plast

pfirst pthis
Deletion Operation on Doubly Circular
Linked list
• Deleting a node from the beginning of the list
• Deleting a node from the end of the list
• Deleting a specified node.
Deleting a node from the beginning of
the Doubly Linear Linked list
1. Check the list is empty or not.
if(head->next==head)
print list is empty and exit
2. Else
a. store the address of second node in the list in temporary pointer “ptemp”
ptemp=head->next->next
b. free the memory occupied by first node
free(head->next)
c. And make the next field of head node point to the node pointed by ptemp
and make the prev field of node pointed by ptemp point to head node .
head->next=ptemp
ptemp->prev=head
4. Exit
Deleting a node from the beginning of the list
11 Copy the next field of first node
pfirst to the pointer ptemp ptemp

ptemp plast
pfirst

NULL

Free the memory occupied by first node


pfirst plast
ptemp

Make the second node as first node


pfirst plast

NULL
NULL
Deleting a node from the end of the list
1. Check the list is empty or not.
if(head->next==head)
print list is empty and exit
2. Else
a. store the address of second last node in the list in temporary
pointer “ptemp”
ptemp=head->prev->prev
b. free the memory occupied by last node
free(head->prev)
c. And make the prev field of head node point to the node
pointed by ptemp
and make the next field of node pointed by ptemp point to
head node .
head->prev=ptemp
ptemp->next=head
4. Exit
Deleting a node from the end of the list

1. Make the second last node to be pointed by “ptemp”


pfirst plast
ptemp
NULL

2. Free the memory occupied by the last node


plast
pfirst ptemp
NULL

3. Make the next field of second last node pointed by “ptemp” to NULL and make the second last node as
last node pfirst plast
NULL
Deleting the specified node
1. Input the specified location “loc” of a node to be deleted
2. Make the new pointer “pthis” point to the node just after head node and
move the pointer “pthis” until it points to the specified node
pthis=head->next
for(i=1;i<=loc-1;i++)
pthis=pthis->next
3. Store the address of the node just after and before the node to be
deleted in a temporary pointer “ptemp1”, “ptemp” respectively
ptemp1=pthis->next
ptemp=pthis->prev
4. Make the next field of node pointed by pointer “ptemp” point to the
node pointed by ptemp1 and make the prev field of node pointed by
pointer “ptemp1” point to the node pointed by ptemp
ptemp->next=ptemp1
ptemp1->prev=ptemp
5. Free the memory occupied by the node to be deleted
free(pthis)
Deleting the specified node
1. Move the pointer “pthis” to the specified node . let the specified
node be Node3
pfirst pthis

Node1 Node2 Node3 Node4


2. Make the next field of node pointed by “ptemp” pointer point to the node just
after the specified node and the pre
ptemp ptemp1
pfirst
NULL

3. Free the memory occupied by the node to be deleted


pfirst
NULL
Advantages of Doubly linked list
• Insertion and Deletion are simple as compared
to other linked list
• Efficient utilization of memory
• Bi-directional traversal helps in efficient and
easy accessibility of nodes
Disadvantages of Doubly linked list
• Uses extra memory compared to singly linked
list as it uses more than one link fields
• Implementing and maintaining doubly linked
lists can be more complex than singly linked
list

You might also like