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

Created with Sketch.

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

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

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

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, 1 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 will have three properties: data, previous which will point to the previous node and next which will point to the next node.
  2. Define another class for creating the doubly linked list, and it has two nodes: head and tail. Initially, head and tail will point to null.
  3. minimumNode() will prints 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. maximumNode() 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. #Represent a node of doubly linked list  
  2. class Node:
  3.     def __init__(self,data):
  4.         self.data = data;
  5.         self.previous = None;
  6.         self.next = None;
  7. class MinMax:
  8.     #Represent the head and tail of the doubly linked list  
  9.     def __init__(self):
  10.         self.head = None;
  11.         self.tail = None;
  12.     #addNode() will add a node to the list  
  13.     def addNode(self, data):
  14.         #Create a new node  
  15.         newNode = Node(data);
  16.         #If list is empty  
  17.         if(self.head == None):
  18.             #Both head and tail will point to newNode  
  19.             self.head = self.tail = newNode;
  20.             #head’s previous will point to None  
  21.             self.head.previous = None;
  22.             #tail’s next will point to None, as it is the last node of the list  
  23.             self.tail.next = None;
  24.         else:
  25.             #newNode will be added after tail such that tail’s next will point to newNode  
  26.             self.tail.next = newNode;
  27.             #newNode’s previous will point to tail  
  28.             newNode.previous = self.tail;
  29.             #newNode will become new tail  
  30.             self.tail = newNode;
  31.             #As it is last node, tail’s next will point to None  
  32.             self.tail.next = None;
  33.     #MinimumNode() will find out minimum value node in the list  
  34.     def minimumNode(self):
  35.         #Node current will point to head  
  36.         current = self.head;
  37.         #Checks if list is empty  
  38.         if(self.head == None):
  39.             print(“List is empty”);
  40.             return 0;
  41.         else:
  42.             #Initially, min will store the value of head’s data  
  43.             min = self.head.data;
  44.             while(current != None):
  45.                 #If value of min is greater than current’s data  
  46.                 #Then, replace value of min with current node’s data  
  47.                 if(min > current.data):
  48.                     min = current.data;
  49.                 current = current.next;
  50.         return min;
  51.     #MaximumNode() will find out maximum value node in the list  
  52.     def maximumNode(self):
  53.         #Node current will point to head  
  54.         current = self.head;
  55.         #Checks if list is empty  
  56.         if(self.head == None):
  57.             print(“List is empty”);
  58.             return 0;
  59.         else:
  60.             #Initially, max will store the value of head’s data  
  61.             max = self.head.data;
  62.             #If value of max is lesser than current’s data  
  63.             #Then, replace value of max with current node’s data  
  64.             while(current != None):
  65.                 if(current.data > max):
  66.                     max = current.data;
  67.                 current = current.next;
  68.         return max;
  69. dList = MinMax();
  70. #Add nodes to the list  
  71. dList.addNode(5);
  72. dList.addNode(7);
  73. dList.addNode(9);
  74. dList.addNode(1);
  75. dList.addNode(2);
  76. #Prints the minimum value node in the list  
  77. print(“Minimum value node in the list: “+ str(dList.minimumNode()));
  78. #Prints the maximum value node in the list  
  79. print(“Maximum value node in the list: “+ str(dList.maximumNode()));

Output:

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

Leave a Reply

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