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

PythonServer Side ProgrammingProgramming

When it is required to delete a node from the end of a 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 'linked_list' class needs to be created that would have an initialization function, and the head of the node would be initialized to 'None'. 

Below is a demonstration for the same −

Example

Live Demo

class Node:  
   def __init__(self,data):  
      self.data = data;  
      self.next = None;  
   
class linked_list:  
   def __init__(self):  
      self.head = Node(None);  
      self.tail = Node(None);  
      self.head.next = self.tail;  
      self.tail.next = self.head;  
     
   def add_value(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;  
     
   def delete_from_end(self):  
      if(self.head == None):  
         return;  
      else:  
         if(self.head != self.tail ):  
            curr = self.head;              
            while(curr.next != self.tail):  
               curr = curr.next;  
            self.tail = curr;  
            self.tail.next = self.head;  
         
         else:  
            self.head = self.tail = None;  
           
   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_list:  
   my_cl = linked_list();  
   my_cl.add_value(11);  
   my_cl.add_value(32);  
   my_cl.add_value(43);  
   my_cl.add_value(57);  

   print("The original list is :");  
   my_cl.print_it();  
   while(my_cl.head != None):  
      my_cl.delete_from_end();  
      print("The list after deletion is :");  
      my_cl.print_it();   

Output

The original list is :
11
32
43
57
The list after deletion is :
11
32
43
The list after deletion is :
11
32
The list after deletion is :
11
The list after deletion is :
The list is empty

Explanation

  • The 'Node' class is created.
  • Another 'linked_list' 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_end' is defined, that deletes elements one by one from the end, 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 'linked_list' class is created, and the methods are called on it to add data.
  • This is displayed on the console using the 'print_it' method.
raja
Published on 13-Mar-2021 10:26:31
Advertisements