Python program to delete a node from the middle of the Circular Linked List

PythonServer Side ProgrammingProgramming

When it is required to delete a node from 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'.  The size variable is initialized to 0.

There will be user defined functions that help add node to the linked list, print them on the console, and delete a node from the middle index.

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 = int(self.size)+1;  
     
   def delete_from_mid(self):  
      if(self.head == None):  
         return;  
      else:  
         count = (self.size//2) if (self.size % 2 == 0) else ((self.size+1)//2);  
         if( self.head != self.tail ):  
            temp = self.head;  
            curr = None;  
            for i in range(0, count-1):  
               curr = temp;  
               temp = temp.next;  
            if(curr != None):  
               curr.next = temp.next;  
               temp = None;  
            else:  
               self.head = self.tail = temp.next;  
               self.tail.next = self.head;  
               temp = None;  
         else:  
            self.head = self.tail = None;  
      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()  
   my_cl.add_data(11)  
   my_cl.add_data(52)  
   my_cl.add_data(36)  
   my_cl.add_data(74)
   print("The original list is :")
   my_cl.print_it()
   while(my_cl.head != None):  
      my_cl.delete_from_mid()
      print("The list after updation is :")
      my_cl.print_it();

Output

The original list is :
11
52
36
74

The list after updation is :
11
36
74

The list after updation is :
11
74

The list after updation is :
74

The list after updation is :
The list is empty

Explanation

  • The 'Node' class is created.
  • Another class with required attributes is created.
  • Another method named 'add_data' is defined, that is used to add data to the circular linked list.
  • Another method named 'delete_from_middle' is defined, that deletes elements one by one from the middle, by removing its reference.
  • Another method named 'print_it' is defined that is used to display the linked list data on the console.
  • An object of the 'list_creation' class is created, and the methods are called on it to add data.
  • The 'delete_from_middle' method is called.
  • It iterates through the nodes in the linked list, gets the middle most index and starts deleting the elements.
  • This is displayed on the console using the 'print_it' method.
raja
Published on 13-Mar-2021 05:59:05
Advertisements