Circular Linked List (CLL)
Circular Linked List (CLL)
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.
• 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: