Circular Linked List
Circular Linked List
In a circular linked list, the last node of the list contains a pointer to the first node of the list. We can have
circular singly linked list as well as circular doubly linked list.
We traverse a circular singly linked list until we reach the same node where we started. The circular singly
liked list has no beginning and no ending. There is no null value present in the next part of any of the
nodes.
Circular linked list are mostly used in task maintenance in operating systems. There are many examples
where circular linked list are being used in computer science including browser surfing where a record of
pages visited in the past by the user, is maintained in the form of circular linked lists and can be accessed
again on clicking the previous button.
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows.
Step 4 - If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Step 5 - Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp →
next == head').
Step 6 - Set 'temp → next = newNode and newNode → next =head', head = newNode.
Step 5 - Keep moving the temp to its next node until it reaches to the last node in the list (until temp →
next == head).
Step 6 - Every time check whether temp is reached to the last node or not. If it is reached to last node
then display 'Given node is not found in the list!!! Insertion not possible!!!' and terminate the
function. Otherwise move the temp to next node.
Step 7 - If temp is reached to the exact node after which we want to insert the newNode then check
whether it is last node (temp → next == head).
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize both
'temp1' and 'temp2' with head.
Step 4 - Check whether list is having only one node (temp1 → next == head)
Step 6 - If it is FALSE move the temp1 until it reaches to the last node. (until temp1 → next == head )
Step 7 - Then set temp1 → next = temp2 → next, head = temp2 → next, and delete temp2.
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.
Step 4 - Check whether list has only one Node (temp1 → next == head)
Step 5 - If it is TRUE. Then, set head = NULL and delete temp1. And terminate from the function.
(Setting Empty list condition)
Step 6 - If it is FALSE. Then, set 'temp2 = temp1 ' and move temp1 to its next node. Repeat the same
until temp1 reaches to the last node in the list. (until temp1 → next == head)
Step 2 - If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
Step 3 - If it is Not Empty then, define two Node pointers 'temp1' and 'temp2' and initialize 'temp1'
with head.
Step 4 - Keep moving the temp1 until it reaches to the exact node to be deleted or to the last node.
And every time set 'temp2 = temp1' before moving the 'temp1' to its next node.
Step 5 - If it is reached to the last node then display 'Given node not found in the list! Deletion not
possible!!!'.
Step 6 - If it is reached to the exact node which we want to delete, then check whether list is having
only one node (temp1 → next == head)
Step 7 - If list has only one node and that is the node to be deleted then set head = NULL and
delete temp1.
Step 08 - If temp1 is not first node and not last node then set temp2 → next = temp1 → next and
delete temp1.