Professional Documents
Culture Documents
,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
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
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
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
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
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