0% found this document useful (0 votes)
12 views36 pages

Circular Linked List (CLL)

Uploaded by

nvkeerthuvcet
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)
12 views36 pages

Circular Linked List (CLL)

Uploaded by

nvkeerthuvcet
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
You are on page 1/ 36

Circularly Linked

List (CLL)
What is CLL?
• In single linked list, every node points to its next node in the sequence
and the last node points NULL.
• But in circularly linked list, every node points to its next node in the
sequence but the last node points to the first node in the list.
• That means circularly linked list is similar to the single linked list
except that the last node points to the first node in the list
Singly Linked List as Circularly

• In singly linked list, the next pointer of the last node points to the first
node.
Doubly Linked List as Circularly
• In doubly linked list, 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
making the circular in both directions.

• As per the above illustration, following are the important points to be


considered.
• The last link's next points to the first link of the list in both cases of singly
as well as doubly linked list.
• The first link's previous points to the last of the list in case of doubly linked
list.
Advantages of Circularly Linked
Lists:
• 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.
• Useful for implementation of queue.
• Circularly lists are useful in applications to repeatedly go around the list. For
example, when multiple applications are running on a PC, it is common for
the operating system to put the running applications on a list and then to
cycle through them, giving each of them a slice of time to execute, and then
making them wait while the CPU is given to another application. It is
convenient for the operating system to use a circular list so that when it
reaches the end of the list it can cycle around to the front of the list.
• Circularly Doubly Linked Lists are used for implementation of advanced data
structures like Fibonacci Heap.
CLL Representation
Operations:
• Insertion
• Deletion
• Searching
• Display
Insertion
• Insertion in an empty list
• At the front
• After a given node
• At the end
Insertion in an empty list
• Initially, when the list is empty, the head pointer will be
NULL.

• After inserting 5
Create a node
At the front
• Create a new node
• If the list is empty, make the new node the head and
point it to itself.
• Otherwise, set the next pointer of the new node to point
to the current head.
• Update the head to point to the new node.
• Update the next pointer of the last node to point to the
new head (to maintain the circular structure).
At the front

• After Insertion
After a given node
• Create a node for inserting
• Traverse the list to find the node at the desired position.
• Update the next pointer of the new node to point to the
next node of the current node.
• Update the next pointer of the current node to point to
the new node.
After a given node

• After insertion
At the end
• Create a new node with the given data.
• If the list is empty, make the new node the head and
point it to itself.
• Otherwise, traverse the list to find the last node.
• Set the next pointer of the last node to point to the new
node.
• Set the next pointer of the new node to point back to
the head (to maintain the circular structure).
At the end

• After insertion
Deletion
• At the front
• At any position
• At the end
At front
• Check if the list is empty. If it is empty, there is nothing to
delete.
• If the list has only one node, set the head to None to
delete the node.
• Otherwise, find the last node of the list (the node whose
next pointer points to the head).
• Update the next pointer of the last node to point to the
second node (head’s next).
• Update the head to point to the second node.
• Optionally, free the memory allocated to the deleted node.
At front
At any position
• Check if the list is empty. If it is empty, there is nothing to
delete.
• If the position is 0, it means deleting the head node. In this
case, update the next pointer of the last node to point to the
second node and update the head.
• Traverse the list to find the node at the desired position
(index – 1).
• Update the next pointer of the current node to skip the node
to be deleted (pointing to the next node of the node to be
deleted).
• Optionally, free the memory allocated to the deleted node.
At any position
At the end
• Check if the list is empty. If it is empty, there is nothing to
delete.
• If the list has only one node, set the head to None to
delete the node.
• Otherwise, find the last node of the list (the node whose
next pointer points to the head).
• Update the next pointer of the last node to point to the
second node (head’s next).
• Update the head to point to the second node.
• Optionally, free the memory allocated to the deleted node.
At last
• previous->next = current-> next
• head = previous -> next
Searching
• If node found, return true
• Otherwise false
Display
Driver code
Output:

You might also like