Python program to insert a new node at the middle of the Circular Linked List

Created with Sketch.

Python program to insert a new node at the middle of the Circular Linked List

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 the 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.

Python 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.

Python 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:
  • It first checks whether the head is null (empty list), then it will insert the node as the head.
  • Both head and tail will point to the newly added node.
  • If the list is not empty, then we calculate size and divide it by 2 to get the mid-point.
  • Define node temp that will point to head and current will point to a node previous to temp.
  • Iterate through the list until the middle of the list is reached by incrementing temp to temp.next.
  • 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.

a. display() will show all the nodes present in the list.

  • Define a new node ‘current’ that will point to the head.
  • Print current.data till current will points to head again.
  • Current will point to the next node in the list in each iteration.

PROGRAM:

  1. #Represents the node of list.  
  2. class Node:
  3.   def __init__(self,data):
  4.     self.data = data;
  5.     self.next = None;
  6. class CreateList:
  7.     #Declaring head and tail pointer as null.  
  8.     def __init__(self):
  9.       self.head = Node(None);
  10.       self.tail = Node(None);
  11.       self.head.next = self.tail;
  12.       self.tail.next = self.head;
  13.       self.size = 0;
  14.     #This function will add the new node to the list.  
  15.     def add(self,data):
  16.        newNode = Node(data);
  17.        #Checks if the list is empty.  
  18.        if self.head.data is None:
  19.          #If list is empty, both head and tail would point to new node.  
  20.          self.head = newNode;
  21.          self.tail = newNode;
  22.          newNode.next = self.head;
  23.        else:
  24.          #tail will point to new node.  
  25.          self.tail.next = newNode;
  26.          #New node will become new tail.  
  27.          self.tail = newNode;
  28.          #Since, it is circular linked list tail will points to head.  
  29.          self.tail.next = self.head;
  30.        #Size will count the number of element in the list  
  31.        self.size = self.size+1;
  32.     #This function will add the new node at the middle of the list.  
  33.     def addInMid(self,data):
  34.         newNode = Node(data);
  35.         #Checks if the list is empty.  
  36.         if(self.head == None):
  37.             #If list is empty, both head and tail would point to new node.  
  38.             self.head = newNode;
  39.             self.tail = newNode;
  40.             newNode.next = self.head;
  41.         else:
  42.             #Store the mid-point of the list  
  43.             count = (self.size//2if (self.size % 2 == 0else ((self.size+1)//2);
  44.             #temp will point to head  
  45.             temp = self.head;
  46.             for i in range(0,count):
  47.                 #Current will point to node previous to temp.  
  48.                 current = temp;
  49.                 #Traverse through the list till the middle of the list is reached  
  50.                 temp = temp.next;
  51.             #current will point to new node  
  52.             current.next = newNode;
  53.             #new node will point to temp  
  54.             newNode.next = temp;
  55.         self.size = self.size+1;
  56.     #Displays all the nodes in the list  
  57.     def display(self):
  58.       current = self.head;
  59.       if self.head is None:
  60.         print(“List is empty”);
  61.         return;
  62.       else:
  63.           #Prints each node by incrementing pointer.  
  64.           print(current.data),
  65.           while(current.next != self.head):
  66.               current = current.next;
  67.               print(current.data),
  68. class CircularLinkedList:
  69.     cl = CreateList();
  70.     #Adds data to the list  
  71.     cl.add(1);
  72.     cl.add(2);
  73.     cl.add(3);
  74.     cl.add(4);
  75.     print(“Original list: “);
  76.     cl.display();
  77.     #Inserting node ‘5’ in the middle  
  78.     cl.addInMid(5);
  79.     print(“\nUpdated List: “);
  80.     cl.display();
  81.     #Inserting node ‘6’ in the middle  
  82.     cl.addInMid(6);
  83.     print(“\nUpdated List: “);
  84.     cl.display();

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *