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

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

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

### Explanation

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

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 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:
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. maxNode() 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

```#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.tail = Node(None);

#This function will add the new node at the end of the list.
newNode = Node(data);
#Checks if the list is empty.
#If list is empty, both head and tail would point to new node.
self.tail = newNode;
else:
#tail will point to new node.
self.tail.next = newNode;
#New node will become new tail.
self.tail = newNode;

#Finds out the minimum value node in the list
def minNode(self):
#Initializing min to initial node data
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;
break;
print("Minimum value node in the list: "+ str(minimum));

#Finds out the maximum value node in the list
def maxNode(self):
#Initializing max to initial node data
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;
break;
print("Maximum value node in the list: "+ str(maximum));

cl = CreateList();
#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
```

### C

```#include <stdio.h>
#include <string.h>
#include <stdlib.h>

//Represents the node of list.
struct node{
int data;
struct node *next;
};

//Declaring head and tail pointer as null.
struct node *tail = NULL;

//This function will add the new node at the end of the list.
//Create new node
struct node *newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = data;
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}else {
//tail will point to new node.
tail->next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Finds out the minimum value node in the list
void minNode() {
//Initializing min to initial node data
printf("\nList is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min > current->data) {
min = current->data;
}
current= current->next;

printf("Minimum value node in the list: %d", min);
}
}

//Finds out the maximum value node in the list
void maxNode() {
//Initializing max to initial node data
printf("\nList is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max < current->data) {
max = current->data;
}
current= current->next;

printf("\nMaximum value node in the list: %d", max);
}
}

int main()
{
//Prints the minimum value node in the list
minNode();
//Prints the maximum value node in the list
maxNode();

return 0;
}
```

Output:

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

### JAVA

```public class MinMax {
//Represents the node of list.
public class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}

//Declaring head and tail pointer as null.
public Node tail = null;

//This function will add the new node at the end of the list.
//Create new node
Node newNode = new Node(data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
tail = newNode;
}
else {
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Finds out the minimum value node in the list
public void minNode() {
//Initializing min to initial node data
System.out.println("List is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min > current.data) {
min = current.data;
}
current= current.next;

System.out.println("Minimum value node in the list: "+ min);
}
}

//Finds out the maximum value node in the list
public void maxNode() {
//Initializing max to initial node data
System.out.println("List is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max < current.data) {
max = current.data;
}
current= current.next;

System.out.println("Maximum value node in the list: "+ max);
}
}

public static void main(String[] args) {
MinMax cl = new MinMax();
//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
```

### C#

```using System;
{
public class Program
{
//Represents the node of list.
public class Node<T>{
public T data;
public Node<T> next;
public Node(T value) {
data = value;
next = null;
}
}

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

//This function will add the new node at the end of the list.
//Create new node
Node<T> newNode = new Node<T>(data);
//Checks if the list is empty.
tail = newNode;
}else{
//tail will point to new node.
tail.next = newNode;
//New node will become new tail.
tail = newNode;
}
}

//Finds out the minimum value node in the list
public void minNode() {
//Initializing min to initial node data
Console.WriteLine("List is empty");
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(min.CompareTo(current.data) > 0) {
min = current.data;
}
current= current.next;

Console.WriteLine("Minimum value node in the list: "+ min);
}
}

//Finds out the maximum value node in the list
public void maxNode() {
//Initializing max to initial node data
Console.WriteLine("List is empty");
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(max.CompareTo(current.data) < 0) {

max = current.data;
}
current= current.next;

Console.WriteLine("Maximum value node in the list: "+ max);
}
}
}

public static void Main()
{

CreateList<int> cl = new CreateList<int>();
//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
```

### PHP

```<!DOCTYPE html>
<html>
<body>
<?php
//Represents the node of list.
class Node{
public \$data;
public \$next;
function __construct(\$data){
\$this->data = \$data;
\$this->next = NULL;
}
}
class CreateList{
//Declaring head and tail pointer as null.
private \$tail;
function __construct(){
\$this->tail = NULL;
}
//This function will add the new node at the end of the list.
//Create new node
\$newNode = new Node(\$data);
//Checks if the list is empty.
//If list is empty, both head and tail would point to new node.
\$this->tail = \$newNode;
}
else{
//tail will point to new node.
\$this->tail->next = \$newNode;
//New node will become new tail.
\$this->tail = \$newNode;
}
}

//Finds out the minimum value node in the list
function minNode() {
//Initializing min to initial node data
echo "List is empty";
}
else {
do{
//If current node's data is smaller than min
//Then replace value of min with current node's data
if(\$min > \$current->data) {
\$min = \$current->data;
}
\$current= \$current->next;

echo "Minimum value node in the list: \$min <br>";
}
}

//Finds out the maximum value node in the list
function maxNode() {
//Initializing max to initial node data
echo "List is empty";
}
else {
do{
//If current node's data is greater than max
//Then replace value of max with current node's data
if(\$max < \$current->data) {
\$max = \$current->data;
}
\$current= \$current->next;

echo "Maximum value node in the list: \$max <br>";
}
}
}

\$cl = new CreateList();
//Prints the minimum value node in the list
\$cl->minNode();
//Prints the maximum value node in the list
\$cl->maxNode();
?>
</body>
</html>
```

Output:

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

Next Topic#