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

python tutorials and learn python

Created with Sketch.

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

In this program, we will create a circular linked list then, iterate through the list to find out the minimum and maximum node.

  1. 9->5->2->7->3

We will maintain two variables min and max. Min will hold the minimum value node, and max will hold the maximum value node. In the above example, 2 will be the minimum value node and 9 will be the maximum value node.

ALGORITHM:

  1. Define a Node class which represents a node in the list. It has two properties data and next which will point to the next node.
  2. Define another class for creating the circular linked list, and it has two nodes: head and tail.
  3. minNode() will print out minimum value node:
  • Define variable min and initialize with head’s data.
  • Current will point to head.
  • Iterate through the list by comparing each node’s data with min.
  • If min > current’s data then min will hold current’s data.
  • At the end of the list, variable min will hold the minimum value node.
  • Print the min value.

a. maxNode() will prints out maximum value node:

  • Define variable max and initialize with head’s data.
  • Current will point to head.
  • Iterate through the list by comparing each node’s data with max.
  • If max > current’s data, then max will hold current’s data.
  • At the end of the list, variable max will hold the maximum value node.
  • Print the max value.

PROGRAM:

  1. #Represents the node of list.  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.next = None;
  6. class CreateList:
  7.     #Declaring head and tail pointer as null.  
  8.     def __init__(self):
  9.         self.head = Node(None);
  10.         self.tail = Node(None);
  11.         self.head.next = self.tail;
  12.         self.tail.next = self.head;
  13.     #This function will add the new node at the end of the list.  
  14.     def add(self,data):
  15.         newNode = Node(data);
  16.         #Checks if the list is empty.  
  17.         if self.head.data is None:
  18.             #If list is empty, both head and tail would point to new node.  
  19.             self.head = newNode;
  20.             self.tail = newNode;
  21.             newNode.next = self.head;
  22.         else:
  23.             #tail will point to new node.  
  24.             self.tail.next = newNode;
  25.             #New node will become new tail.  
  26.             self.tail = newNode;
  27.             #Since, it is circular linked list tail will point to head.  
  28.             self.tail.next = self.head;
  29.     #Finds out the minimum value node in the list  
  30.     def minNode(self):
  31.         current = self.head;
  32.         #Initializing min to initial node data  
  33.         minimum = self.head.data;
  34.         if(self.head == None):
  35.             print(“List is empty”);
  36.         else:
  37.             while(True):
  38.                 #If current node’s data is smaller than min  
  39.                 #Then replace value of min with current node’s data  
  40.                 if(minimum > current.data):
  41.                     minimum = current.data;
  42.                 current= current.next;
  43.                 if(current == self.head):
  44.                     break;
  45.         print(“Minimum value node in the list: “+ str(minimum));
  46.     #Finds out the maximum value node in the list  
  47.     def maxNode(self):
  48.         current = self.head;
  49.         #Initializing max to initial node data  
  50.         maximum = self.head.data;
  51.         if(self.head == None):
  52.             print(“List is empty”);
  53.         else:
  54.             while(True):
  55.                 #If current node’s data is greater than max  
  56.                 #Then replace value of max with current node’s data  
  57.                 if(maximum < current.data):
  58.                     maximum = current.data;
  59.                 current= current.next;
  60.                 if(current == self.head):
  61.                     break;
  62.         print(“Maximum value node in the list: “+ str(maximum));
  63. class CircularLinkedList:
  64.     cl = CreateList();
  65.     #Adds data to the list  
  66.     cl.add(5);
  67.     cl.add(20);
  68.     cl.add(10);
  69.     cl.add(1);
  70.     #Prints the minimum value node in the list  
  71.     cl.minNode();
  72.     #Prints the maximum value node in the list  
  73.     cl.maxNode();

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *