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 −
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()
Values have been added to the list The maximum valueed node is : 74 Minimum value node in the list: 11