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



When it is required to insert a new node at the middle of the circular linked list, a 'Node' class needs to be created. In this class, there are two attributes, the data that is present in the node, and the access to the next node of the linked list.

In a circular linked list, the head and the rear are adjacent to each other. They are connected to form a circle, and don't have 'NULL' value in the last node.

Another class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'. 

Multiple methods are defined by the user to add node in between the linked list, and to print the node values.

Below is a demonstration for the same −

Example

Live Demo

class Node:      def __init__(self,data):         self.data = data       self.next = None       class list_creation:      def __init__(self):         self.head = Node(None)         self.tail = Node(None)         self.head.next = self.tail         self.tail.next = self.head         self.size = 0;      def add_data(self,my_data):         new_node = Node(my_data)       if self.head.data is None:          self.head = new_node          self.tail = new_node          new_node.next = self.head         else:            self.tail.next = new_node          self.tail = new_node          self.tail.next = self.head       self.size = self.size+1    def add_in_between(self,my_data):         new_node = Node(my_data);         if(self.head == None):            self.head = new_node;            self.tail = new_node;            new_node.next = self.head;         else:            count = (self.size//2) if (self.size % 2 == 0) else ((self.size+1)//2);            temp = self.head;            for i in range(0,count):               curr = temp;               temp = temp.next;            curr.next = new_node;            new_node.next = temp;         self.size = self.size+1;              def print_it(self):         curr = self.head;         if self.head is None:            print("The list is empty");            return;         else:          print(curr.data)          while(curr.next != self.head):               curr = curr.next;               print(curr.data)          print("\n");       class circular_linked_list:      my_cl = list_creation()    print("Nodes are being added to the list")    my_cl.add_data(21)    my_cl.add_data(54)    my_cl.add_data(78)    my_cl.add_data(99)    print("The list is :")    my_cl.print_it();      my_cl.add_in_between(33);    print("The updated list is :")    my_cl.print_it();      my_cl.add_in_between(56);    print("The updated list is :")    my_cl.print_it();      my_cl.add_in_between(0);    print("The updated list is :")    my_cl.print_it();   

Output

Nodes are being added to the list The list is : 21 54 78 99 The updated list is : 21 54 33 78 99 The updated list is : 21 54 33 56 78 99 The updated list is : 21 54 33 0 56 78 99

Explanation

  • The 'Node' class is created.
  • Another class with required attributes is created.
  • Another method named 'add_in_between' is defined, that is used to add data to the circular linked list in the middle, i.e at the middle most position.
  • Another method named 'print_it' is defined, that displays the nodes of the circular linked list.
  • An object of the 'list_creation' class is created, and the methods are called on it to add data.
  • An 'init' method is defined, that the first and last nodes of the circular linked list to None.
  • The 'add_in_between' method is called.
  • It iterates through the list, and gets the middle most index and inserts the element in this position.
  • This is displayed on the console using the 'print_it' method.
Updated on: 2021-03-13T06:06:01+05:30

364 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements