0% found this document useful (0 votes)
786 views24 pages

6 Circular Doubly Linked List

A circular doubly linked list combines features of a doubly linked list and a circular linked list. Each node contains pointers to the previous and next nodes, and the last node's next pointer links to the first node, forming a circle. This allows traversal in either direction without a header node. Key operations on a circular doubly linked list include insertion and deletion at the beginning or end by updating the pointers between affected nodes and the header.

Uploaded by

Mohammad Junaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
786 views24 pages

6 Circular Doubly Linked List

A circular doubly linked list combines features of a doubly linked list and a circular linked list. Each node contains pointers to the previous and next nodes, and the last node's next pointer links to the first node, forming a circle. This allows traversal in either direction without a header node. Key operations on a circular doubly linked list include insertion and deletion at the beginning or end by updating the pointers between affected nodes and the header.

Uploaded by

Mohammad Junaid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Circular Doubly Linked List

(or)
Circular Two-way Linked
List
Circular Double Linked List
• It combines features from both Doubly Linked list and
Circular linked list.
• In Circular double Linked list, 2 consecutive nodes are
linked by NEXT and PREVIOUS pointers, and the next
field of the last node stores the address of the first node
of the list and the previous field of the first node stores
the address of the last node of the list.
• Representation of Circular Double Linked List
Memory representation of a
circular doubly linked list

Structure of a node in
Circular Double Linked
List

PREVIOUS DATA NEXT


Basic Operations on Circular Doubly Linked List
• Insertion
• Deletion
• Traverse

The node structure of a Circular doubly linked list in C,

struct node
{
struct node *prev;
int data;
struct node *next;
};
Insertion on Circular Doubly Linked List

• Case 1: Inserting a new node at


the beginning.
• Case 2: Inserting a new node at
the end.

Rest of the cases are similar to that given


for doubly linked lists.
Inserting a New Node at the Beginning of a Circular
Doubly Linked List New Node
9
Header Given CDLL

5 20 12

Header
Ptr

5 20 12
Move Ptr so that it points to the last node of the list.

Header Ptr

5 20 12

Insert the new node in between Ptr and the Header node.

Header Ptr
2
3
9 5 20 12
4
1
Move the Header to point to the new node.

Header

2
3
9 5 20 12
4
1
Algorithm to insert a new node at front of Circular Doubly
Linked List

Step 1: Create New node


Step 2: SET newNodeDATA = value
Step 3: SET Ptr= Header
Step 4: Repeat Step 5 while Ptr  NEXT != Header
Step 5: SET Ptr = Ptr  NEXT
[END OF LOOP]
Step 6 : SET Ptr  NEXT = newNode
Step 7: SET newNodePREV = Ptr
Step 8: SET newNodeNEXT = Header
Step 9: SET HeaderPREV = newNode
Step 10: SET Header = newNode
Step 11: Stop
Inserting a New Node at the End of a Circular Doubly
Linked List New Node
9
Header Given CDLL

5 20 12

Header
Ptr

5 20 12
Move Ptr so that it points to the last node of the list.

Header Ptr

5 20 12

Insert the new node in between Ptr and the Header node. So that new node will be
last node.

Header Ptr
4
2
5 20 12 9
3
1
Algorithm to insert a new node at the END of Circular Doubly
Linked List

Step 1: Create New node


Step 2: SET newNodeDATA = value
Step 3: SET Ptr= Header
Step 4: Repeat Step 5 while Ptr  NEXT != Header
Step 5: SET Ptr = Ptr  NEXT
[END OF LOOP]
Step 6: SET newNodeNEXT = Header
Step 7 : SET Ptr  NEXT = newNode
Step 8: SET newNodePREV = Ptr
Step 9: SET HeaderPREV = newNode
Step 10:
Stop
Deletion on Circular Doubly Linked List

• Case 1: Deleting a node at the


beginning.
• Case 2: Deleting a node at the end.

Rest of the cases are similar to that given


for doubly linked lists.
Deleting a Node at the Beginning of a Circular Doubly
Linked List
Node to be deleted= 5

Header Given CDLL

5 20 12

Header
Ptr

5 20 12
Move Ptr so that it points to the last node of the list.

Header Ptr

5 20 12

Update pointers

Header Ptr
2

5 20 12

1
Free the front node of the list.

Header Ptr

5 20 12

Make the Header to Point to new front node

Header Ptr

5 20 12
Final Circular Double Linked List after removing front
node with data 5

Header Ptr

20 12
Algorithm to delete a node at front of Circular Doubly Linked
List

Step 1: IF Header = NULL


Print “List is empty”
Go to Step 9
[END OF IF]
Step 2: SET Ptr =
Step 3: Header
Step 4: Repeat Step 4 while Ptr NEXT != Header
SET Ptr = Ptr  NEXT
Step 5: [END OF LOOP]
Step 6: SET Ptr  NEXT = Header NEXT
Step 7: SET Header  NEXT  PREV =
Step 8: Ptr FREE Header
Step 9: SET Header = Ptr  NEXT
Stop
Deleting a Node at the End of a Circular Doubly Linked
List
Node to be deleted= 12

Header Given CDLL

5 20 12

Header
Ptr

5 20 12
Move Ptr so that it points to the last node of the list.

Header Ptr

5 20 12

Update pointers

Header Ptr
2

5 20 12
1
Free the node pointed by Ptr

Header
Ptr

5 20 12

Final Circular Double Linked List after removing LAST node with data 12

Header

5 20
Algorithm to delete a node at END of Circular Doubly Linked
List

Step 1: IF Header = NULL


Print “List is empty”
Go to Step 8
[END OF IF]
Step 2: SET Ptr =
Step 3: Header
Step 4: Repeat Step 4 while Ptr NEXT != Header
SET Ptr = Ptr  NEXT
Step 5: [END OF LOOP]
Step 6: SET Ptr PREV NEXT = Header
Step 7: SET Header  PREV = Ptr PREV
Step 8: FREE Ptr
Stop
Traversing Circular Double Linked List
• Traversing a linked list means accessing all the nodes of the list
in order to perform some processing on them.
• Example: Displaying the contents of Linked list,
Counting
number of nodes in the linked list, etc..

Algorithm for Displaying Circular Double Linked List:


Step -1: ptr = header
Step-2:
Step-3: Repeat Steps-3
print andptr
4 while ptr Next!=
 data
Header
Step- Set ptr = ptr
4: next
Step- 5: print [END OF data
ptr  LOOP]#prints last node
Step -6: Stop
Given CDLL

Header
Ptr

5 20 12

Output : 5 20 12

You might also like