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

In this program, you'll be learning Python program to find the maximum and minimum value node from a circular linked list.

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

Here in this python program youll be learning how to find the maximum and minimum value node from a circular linked list.

In this program first, we’ll create a circular link list and then we’ll sort maximum and minimum from this created linked list.

Program to find maximum and minimum value node from a circular linked list

#Represents the node of list.    
class Node:    
    def __init__(self,data):    
        self.data = data;    
        self.next = None;    
     
class CreateList:    
    #Declaring head and tail pointer as null.    
    def __init__(self):    
        self.head = Node(None);    
        self.tail = Node(None);    
        self.head.next = self.tail;    
        self.tail.next = self.head;    
        
    #This function will add the new node at the end of the list.    
    def add(self,data):    
        newNode = Node(data);    
        #Checks if the list is empty.    
        if self.head.data is None:    
            #If list is empty, both head and tail would point to new node.    
            self.head = newNode;    
            self.tail = newNode;    
            newNode.next = self.head;    
        else:    
            #tail will point to new node.    
            self.tail.next = newNode;    
            #New node will become new tail.    
            self.tail = newNode;    
            #Since, it is circular linked list tail will point to head.    
            self.tail.next = self.head;    
                
    #Finds out the minimum value node in the list    
    def minNode(self):    
        current = self.head;    
        #Initializing min to initial node data    
        minimum = self.head.data;    
        if(self.head == None):    
            print("List is empty");    
        else:    
            while(True):    
                #If current node's data is smaller than min    
                #Then replace value of min with current node's data    
                if(minimum > current.data):    
                    minimum = current.data;    
                current= current.next;    
                if(current == self.head):    
                    break;    
        print("Minimum value node in the list: "+ str(minimum));    
     
    #Finds out the maximum value node in the list    
    def maxNode(self):    
        current = self.head;    
        #Initializing max to initial node data    
        maximum = self.head.data;    
        if(self.head == None):    
            print("List is empty");    
        else:    
            while(True):    
                #If current node's data is greater than max    
                #Then replace value of max with current node's data    
                if(maximum < current.data):    
                    maximum = current.data;    
                current= current.next;    
                if(current == self.head):    
                    break;    
        print("Maximum value node in the list: "+ str(maximum));    
     
class CircularLinkedList:    
    cl = CreateList();    
    #Adds data to the list    
    cl.add(5);    
    cl.add(20);    
    cl.add(10);    
    cl.add(1);    
    #Prints the minimum value node in the list    
    cl.minNode();    
    #Prints the maximum value node in the list    
    cl.maxNode();

Output

Minimum value node in the list: 1
Maximum value node in the list: 20

Related Program

  1. Python Program to find the Square Roots
  2. Python Program to Shuffle Deck of Cards
  3. Python Program to delete a new node from the middle of the doubly linked list.
  4. Python program convert decimal number to binary number using recursion
  5. Python Program to Find Factorial of a Number Using Recursion
  6. Python Program to Check if a Number is Positive, Negative or 0
  7. Python program convert decimal number to binary number using recursion
  8. Python Program to Find Factorial of a Number Using Recursion
  9. Python Program to Check if a Number is Positive, Negative or 0
  10. Python Program to Display Fibonacci Sequence Using Recursion

Ask your questions and clarify your/others doubts on how to find maximum and minimum value node from a circular linked list by commenting. Python Documentation