Home » Program to Insert a New Node at the Middle of the Circular Linked List

Program to Insert a New Node at the Middle of the Circular Linked List

by Online Tutorials Library

Q. Program to insert a new node at the middle of the circular linked list.

Explanation

In this program, we create a circular linked list and insert a new node in the middle of the list. If the list is empty, both head and tail will point to new node. If the list is not empty, then we will calculate the size of the list and divide it by 2 to get the mid-point of the list where the new node needs to be inserted.

Program to insert a new node at the middle of the circular linked list

After inserting the new node in the middle of the list.

Program to insert a new node at the middle of the circular linked list

Consider the above diagram; the new node needs to be added to the middle of the list. First, we calculate the size which in this case is 4. So, to get the mid-point, we divide it by 2 and store it in a variable count. We will define two nodes current, and temp such that temp will point to head, and current will point to the node previous to temp. We iterate through the list till mid-point is reached by incrementing temp to temp.next then, insert the new node in between current and temp. Current’next node will be new and the new’s next node will be temp.

Algorithm

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list and it has two nodes: head and tail. Variable size stores the size of the list. It has two methods: addInMid() and display() .
  3. addInMid() will add the node to the middle of the list:
    1. It first checks whether the head is null (empty list), then it will insert the node as the head.
    2. Both head and tail will point to the newly added node.
    3. If the list is not empty, then we calculate size and divide it by 2 to get the mid-point.
    4. Define node temp that will point to head and current will point to a node previous to temp.
    5. Iterate through the list until the middle of the list is reached by incrementing temp to temp.next.
    6. The new node will be inserted after current and before temp such that current will point to the new node and the new node will point to temp.
  4. display() will show all the nodes present in the list.
    1. Define a new node ‘current’ that will point to the head.
    2. Print current.data till current will points to head again.
    3. Current will point to the next node in the list in each iteration.

Solution

Python

Output:

Original list:   1 2 3 4  Updated List:   1 2 5 3 4  Updated List:   1 2 5 6 3 4  

C

Output:

Original list:   1 2 3 4  Updated List:   1 2 5 3 4  Updated List:   1 2 5 6 3 4  

JAVA

Output:

Original list:   1 2 3 4  Updated List:   1 2 5 3 4  Updated List:   1 2 5 6 3 4  

C#

Output:

Original list:   1 2 3 4  Updated List:   1 2 5 3 4  Updated List:   1 2 5 6 3 4  

PHP

Output:

Original list:   1 2 3 4  Updated List:   1 2 5 3 4  Updated List:   1 2 5 6 3 4  

Next Topic#

You may also like