In this tutorial, we will see all about the insert operation in a circular linked list. In the insert operation, we will add a new node to the circular linked list at any position.
In a Circular Linked List, we can insert a node at the following positions:
At the beginning of the list.
At the end of the list.
At the specific position of the list.
1. Insert a node at the beginning of the list.
To insert a new node at the beginning or front position in the list, we need to follow these steps:
Create a newNode with given value.
Check whether list is Empty (head == NULL)
If it is Empty then, set head = newNode and newNode→next = head .
If it is Not Empty then, define a Node pointer 'temp' and initialize with 'head'.
Keep moving the 'temp' to its next node until it reaches to the last node (until 'temp → next == head').
Set 'newNode → next =head', 'head = newNode' and 'temp → next = head'.
In the below given example we will see how to insert a new node in the beginning or front the linked list.
Output
Original list:
20 30 40 50
List after inserting 10 at the beginning:
10 20 30 40 50
2. Insert a node at the end of the circular linked list.
To insert a new node at the end or last position in the circular linked list, we need to follow these steps:
Create a newNode with given value.
Check whether list is Empty (head == NULL).
If it is Empty then, set head = newNode and newNode → next = head.
If it is Not Empty then, define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next == head).
Set temp → next = newNode and newNode → next = head.
In the below given example we will see how to insert a new node at the end of the circular linked list.
Output
Original list:
20 30 40 50
List after inserting 80 at the end of the list:
20 30 40 50 80
3. Insert a node at a specific position of the list
To insert a new node at the specific position of the circular linked list, we need to follow the given steps.
Create a newNode with given value.
Check whether list is Empty (head == NULL)
If it is Empty then, set head = newNode and newNode → next = head.
If it is Not Empty then, define a node pointer temp and initialize with head.
Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).
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.
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).
If temp is last node then set temp → next = newNode and newNode → next = head.
If temp is not last node then set newNode → next = temp → next and temp → next = newNode.
In the below given example we will see how to insert a new node in the specific position in the circular linked list.
Output
Original list: 20 30 40 50
List after insertions: 20 30 99 40 50
That's all, guys. I hope this article is helpful for you.
Happy Learning... 😀
Please share this article on social media to help others.
If you have any queries or suggestions regarding this article, please share with us. feedback@javabytechie.com