Python program to find the maximum and minimum value node from a circular linked list


When it is required to find the maximum and minimum node values from 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 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 to the linked list, to find the minimum and maximum values in the nodes and display them.

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  

   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  

   def find_min_node(self):  
      curr = self.head;  
      min_val = self.head.data;  
      if(self.head == None):  
         print("The list is empty");  
      else:  
         while(True):  
            if(min_val > curr.data):  
               min_val = curr.data;  
            curr= curr.next;  
            if(curr == self.head):  
               break;  
      print("Minimum value node in the list: "+ str(min_val));  
   
   def find_max_node(self):  
      curr = self.head;  
      max_val = self.head.data;  
      if(self.head == None):  
         print("List is empty");  
      else:  
         while(True):  
            if(max_val < curr.data):  
               max_val = curr.data;  
            curr= curr.next;  
            if(curr == self.head):  
               break;  
      print("The maximum valueed node is : "+ str(max_val));
   
class circular_linked_list:  
   my_cl = list_creation()
   print("Values have been added to the list")
   my_cl.add_data(11)  
   my_cl.add_data(52)  
   my_cl.add_data(36)  
   my_cl.add_data(74)  
   my_cl.find_max_node()
   my_cl.find_min_node()

Output

Values have been added to the list
The maximum valueed node is : 74
Minimum value node in the list: 11

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 'find_max_node' is defined, that iterates through the list and gets the maximum value in a node.
  • Another method named 'find_min_node' is defined, that iterates through the list and gets the minimum value in a node.
  • 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 'find_max_node' method and the 'find_min_node' methods are called.
  • It iterates through the nodes in the linked list, gets the maximum and the minimum value in the list.
  • This is displayed on the console.

Updated on: 13-Mar-2021

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements