# Program to Find the Maximum and Minimum Value Node From a Doubly Linked List

Program to Find the Maximum and Minimum Value Node From a Doubly Linked List on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

## Q. Program to find the maximum and minimum value node from a doubly linked list.

### Explanation

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

We will maintain two variables min and max. Min will hold the minimum value node, and max will hold the maximum value node. In 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:
1. Define variable min and initialize with head's data.
2. Current will point to head.
3. Iterate through the list by comparing each node's data with min.
4. If min > current's data then min will hold current's data.
5. At the end of the list, variable min will hold the minimum value node.
6. Print the min value.
4. maximumNode() will prints out maximum value node:
1. Define variable max and initialize with head's data.
2. Current will point to head.
3. Iterate through the list by comparing each node's data with max.
4. If max < current's data then max will hold current?s data.
5. At the end of the list, variable max will hold the maximum value node.
6. Print the max value.

## Solution

### Python

```#Represent a node of doubly linked list
class Node:
def __init__(self,data):
self.data = data;
self.previous = None;
self.next = None;

class MinMax:
def __init__(self):
self.tail = None;

#Create a new node
newNode = Node(data);

#If list is empty
#Both head and tail will point to newNode
#head's previous will point to None
#tail's next will point to None, as it is the last node of the list
self.tail.next = None;
else:
#newNode will be added after tail such that tail's next will point to newNode
self.tail.next = newNode;
#newNode's previous will point to tail
newNode.previous = self.tail;
#newNode will become new tail
self.tail = newNode;
#As it is last node, tail's next will point to None
self.tail.next = None;

#MinimumNode() will find out minimum value node in the list
def minimumNode(self):
#Node current will point to head

#Checks if list is empty
print("List is empty");
return 0;
else:
#Initially, min will store the value of head's data
while(current != None):
#If value of min is greater than current's data
#Then, replace value of min with current node's data
if(min > current.data):
min = current.data;
current = current.next;
return min;

#MaximumNode() will find out maximum value node in the list
def maximumNode(self):
#Node current will point to head

#Checks if list is empty
print("List is empty");
return 0;
else:
#Initially, max will store the value of head's data
#If value of max is lesser than current's data
#Then, replace value of max with current node's data
while(current != None):
if(current.data > max):
max = current.data;
current = current.next;
return max;

dList = MinMax();

#Prints the minimum value node in the list
print("Minimum value node in the list: "+ str(dList.minimumNode()));
#Prints the maximum value node in the list
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
```

### C

```#include <stdio.h>

//Represent a node of the doubly linked list

struct node{
int data;
struct node *previous;
struct node *next;
};

struct node *head, *tail = NULL;

//Create a new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;

//If list is empty
//Both head and tail will point to newNode
//head's previous will point to NULL
//tail's next will point to NULL, as it is the last node of the list
tail->next = NULL;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail->next = newNode;
//newNode's previous will point to tail
newNode->previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to NULL
tail->next = NULL;
}
}

//MinimumNode() will find out minimum value node in the list
int minimumNode() {
//Node current will point to head
int min;

//Checks if list is empty
printf("List is empty\n");
return 0;
}
else {
//Initially, min will store the value of head's data
while(current != NULL) {
//If value of min is greater than current's data
//Then, replace value of min with current node's data
if(min > current->data)
min = current->data;
current = current->next;
}
}
return min;
}

//MaximumNode() will find out maximum value node in the list
int maximumNode() {
//Node current will point to head
int max;

//Checks if list is empty
printf("List is empty\n");
return 0;
}
else {
//Initially, max will store the value of head's data
//If value of max is lesser than current's data
//Then, replace value of max with current node's data
while(current != NULL) {
if(current->data > max)
max = current->data;
current = current->next;
}
}
return max;
}

int main()
{

//Prints the minimum value node in the list
printf("Minimum value node in the list: %d\n", minimumNode());
//Prints the maximum value node in the list
printf("Maximum value node in the list: %d", maximumNode());

return 0;
}
```

Output:

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

### JAVA

```public class MinMax {

//Represent a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {
this.data = data;
}
}

//Create a new node
Node newNode = new Node(data);

//If list is empty
//Both head and tail will point to newNode
//head's previous will point to null
//tail's next will point to null, as it is the last node of the list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}

//MinimumNode() will find out minimum value node in the list
public int minimumNode() {
//Node current will point to head
int min;

//Checks if list is empty
System.out.println("List is empty");
return 0;
}
else {
//Initially, min will store the value of head's data
while(current != null) {
//If the value of min is greater than the current's data

//Then, replace the value of min with current node's data

if(min > current.data)
min = current.data;
current = current.next;
}
}
return min;
}

//MaximumNode() will find out maximum value node in the list
public int maximumNode() {
//Node current will point to head
int max;

//Checks if list is empty
System.out.println("List is empty");
return 0;
}
else {
//Initially, max will store the value of head's data
//If value of max is lesser than current's data
//Then, replace value of max with current node's data
while(current != null) {
if(current.data > max)
max = current.data;
current = current.next;
}
}
return max;
}

public static void main(String[] args) {

MinMax dList = new MinMax();

//Prints the minimum value node in the list
System.out.println("Minimum value node in the list: "+ dList.minimumNode());
//Prints the maximum value node in the list
System.out.println("Maximum value node in the list: "+ dList.maximumNode());
}
}
```

Output:

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

### C#

```using System;
{
public class Program
{
//Represent a node of the doubly linked list

public class Node<T>{
public T data;
public Node<T> previous;
public Node<T> next;

public Node(T value) {
data = value;
}
}

public class MinMax<T> where T : IComparable<T>{
protected Node<T> tail = null;

//Create a new node
Node<T> newNode = new Node<T>(data);

//If list is empty
//Both head and tail will point to newNode
//head's previous will point to null
//tail's next will point to null, as it is the last node of the list
tail.next = null;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
//newNode's previous will point to tail
newNode.previous = tail;
//newNode will become new tail
tail = newNode;
//As it is last node, tail's next will point to null
tail.next = null;
}
}

//MinimumNode() will find out minimum value node in the list
public T minimumNode() {
//Node current will point to head
T min;

//Checks if list is empty
Console.WriteLine("List is empty");
return default(T);
}
else {
//Initially, min will store the value of head's data
while(current != null) {
//If value of min is greater than current's data
//Then, replace value of min with current node's data
if(min.CompareTo(current.data) > 0)
min = current.data;
current = current.next;
}
}
return min;
}

//MaximumNode() will find out maximum value node in the list
public T maximumNode() {
//Node current will point to head
T max;

//Checks if list is empty
Console.WriteLine("List is empty");
return default(T);
}
else {
//Initially, max will store the value of head's data
//If value of max is lesser than current's data
//Then, replace value of max with current node's data
while(current != null) {
if(current.data.CompareTo(max) > 0)
max = current.data;
current = current.next;
}
}
return max;
}
}

public static void Main()
{
MinMax<int> dList = new MinMax<int>();

//Prints the minimum value node in the list
Console.WriteLine("Minimum value node in the list: "+ dList.minimumNode());
//Prints the maximum value node in the list
Console.WriteLine("Maximum value node in the list: "+ dList.maximumNode());
}
}
}
```

Output:

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

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represent a node of doubly linked list
class Node{
public \$data;
public \$previous;
public \$next;

function __construct(\$data){
\$this->data = \$data;
}
}
class MinMax{
public \$tail;
function __construct(){
\$this->tail = NULL;
}

//Create a new node
\$newNode = new Node(\$data);

//If list is empty
//Both head and tail will point to newNode
//head's previous will point to NULL
//tail's next will point to NULL, as it is the last node of the list
\$this->tail->next = NULL;
}
else {
//newNode will be added after tail such that tail's next will point to newNode
\$this->tail->next = \$newNode;
//newNode's previous will point to tail
\$newNode->previous = \$this->tail;
//newNode will become new tail
\$this->tail = \$newNode;
//As it is last node, tail's next will point to NULL
\$this->tail->next = NULL;
}
}

//MinimumNode() will find out minimum value node in the list
function minimumNode() {
//Node current will point to head

//Checks if list is empty
print("List is empty <br>");
return 0;
}
else {
//Initially, min will store the value of head's data
while(\$current != NULL) {
//If value of min is greater than current's data
//Then, replace value of min with current node's data
if(\$min > \$current->data)
\$min = \$current->data;
\$current = \$current->next;
}
}
return \$min;
}

//MaximumNode() will find out maximum value node in the list
function maximumNode() {
//Node current will point to head

//Checks if list is empty
print("List is empty <br>");
return 0;
}
else {
//Initially, max will store the value of head's data
//If value of max is lesser than current's data
//Then, replace value of max with current node's data
while(\$current != NULL) {
if(\$current->data > \$max)
\$max = \$current->data;
\$current = \$current->next;
}
}
return \$max;
}
}

\$dList = new MinMax();

//Prints the minimum value node in the list
print("Minimum value node in the list: " . \$dList->minimumNode());
print("<br>");
//Prints the maximum value node in the list
print("Maximum value node in the list: " . \$dList->maximumNode());
?>
</body>
</html>
```

Output:

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

Next Topic#