Circular Singly Linked List
Circular Singly Linked List
Operations
In a circular linked list, we perform the following operations...
1. Insertion
2. Deletion
3. Display
Before we implement actual operations, first we need to setup empty list. First perform the following steps before
implementing actual operations.
1. Include all the head files which are used in the program.
2. Declare all the user defined functions.
3. Define a Node structure with two members data and next
4. Define a Node pointer 'head' and set it to NULL.
5. Implement the main method by displaying operations menu and make suitable function calls in the main method to
perform user selected operation.
Insertion
In a circular linked list, the insertion operation can be performed in three ways. They are as follows...
Inserting At Beginning of the list
Inserting At End of the list
Inserting At Specific location in the list
1
Inserting at End of the list
Steps to insert a new node at end of the circular linked list...
1. Step 1: Create a newNode with given value.
2. Step 2: Check whether list is Empty (head == NULL).
3. Step 3: If it is Empty then, set head = newNode and newNode . next = head.
4. Step 4: If it is Not Empty then, define a node pointer tmp and initialize with head.
5. Step 5: Keep moving the tmp to its next node until it reaches to the last node in the list (until tmp. next == head).
6. Step 6: Set tmp. next = newNode and newNode . next = head.
2
6. Step 6: Every time check whether tmp 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
tmp to next node.
7. Step 7: If tmp is reached to the exact node after which we want to insert the newNode then check whether it is last
node (currentNode. next == head).
8. Step 8: If tmp is last node then set tmp. next = newNode and newNode. next = head.
9. Step 8: If tmp is not last node then set newNode. next = tmp. next and tmp. next = newNode.
Deletion
In a circular linked list, the deletion operation can be performed in three ways those are as follows...
1. Deleting from Beginning of the list
2. Deleting from End of the list
3. Deleting a Specific Node
3
Deleting from End of the list
Steps to delete a node from end of the circular linked list...
1. Step 1: Check whether list is Empty (head == NULL)
2. Step 2: If it is Empty then, display 'List is Empty!!! Deletion is not possible' and terminate the function.
3. Step 3: If it is Not Empty then, define two Node pointers 'tmp1' and 'tmp2' and initialize 'tmp1' with head.
4. Step 4: Check whether list has only one Node (tmp1 . next == head)
5. Step 5: If it is TRUE. Then, set head = NULL and delete tmp1. And terminate from the function. (Setting Empty list
condition)
6. Step 6: If it is FALSE. Then, set 'tmp2 = tmp1 ' and move tmp1 to its next node. Repeat the same until tmp1 reaches
to the last node in the list. (until tmp1 . next == head)
7. Step 7: Set tmp2.next = head and delete tmp1.
4
5. Step 5: If it is reached to the last node then display 'Given node not found in the list! Deletion not possible!!!'.
And terminate the function.
6. Step 6: If it is reached to the exact node which we want to delete, then check whether list is having only one
node (tmp1 . next == head)
7. Step 7: If list has only one node and that is the node to be deleted then set head = NULL and delete tmp1.
8. Step 8: If list contains multiple nodes then check whether tmp1 is the head node in the list (tmp1 == head).
9. Step 9: If tmp1 is the head node then set tmp2 = head and keep moving tmp2 to its next node until tmp2
reaches to the last node. Then set head = head.next, tmp2.next = head and delete tmp1.
10. Step 10: If tmp1 is not head node then check whether it is last node in the list (tmp1.next == head).
11. Step 11: If tmp1 is last node then set tmp2.next = head and delete tmp1.
12. Step 12: If tmp1 is not head node and not last node then set tmp2.next = tmp1.next and delete tmp1.