TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

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.

<< Back to PROGRAM

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.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

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 *head = NULL;
struct node *tail = NULL;
 
//This function will add the new node at the end of the list.
void add(int data){
    //Create new node
    struct node *newNode = (struct node*)malloc(sizeof(struct node));
    newNode->data = data;
    //Checks if the list is empty.
    if(head == NULL){
        //If list is empty, both head and tail would point to new node.
        head = newNode;
        tail = newNode;
        newNode->next = head;
    }else {
        //tail will point to new node.
        tail->next = newNode;
        //New node will become new tail.
        tail = newNode;
        //Since, it is circular linked list tail will point to head.
        tail->next = head;
    }
}
 
//Finds out the minimum value node in the list
void minNode() {
    struct node *current = head;
    //Initializing min to initial node data
    int min = head->data;
    if(head == NULL) {
        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;
        }while(current != head);
        
        printf("Minimum value node in the list: %d", min);
    }
}
        
//Finds out the maximum value node in the list
void maxNode() {
    struct node *current = head;
    //Initializing max to initial node data
    int max = head->data;
    if(head == NULL) {
        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;
            }while(current != head);
         
        printf("\nMaximum value node in the list: %d", max);
    }
}
    
int main()
{
    //Adds data to the list
    add(5);
    add(20);
    add(10);
    add(1);
    //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 head = null;
    public Node tail = null;
    
    //This function will add the new node at the end of the list.
    public void add(int data){
        //Create new node
        Node newNode = new Node(data);
        //Checks if the list is empty.
        if(head == null) {
             //If list is empty, both head and tail would point to new node.
            head = newNode;
            tail = newNode;
            newNode.next = head;
        }
        else {
            //tail will point to new node.
            tail.next = newNode;
            //New node will become new tail.
            tail = newNode;
            //Since, it is circular linked list tail will points to head.
            tail.next = head;
        }
    }
    
    //Finds out the minimum value node in the list
    public void minNode() {
        Node current = head;
        //Initializing min to initial node data
        int min = head.data;
        if(head == null) {
            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;
            }while(current != head);
        
            System.out.println("Minimum value node in the list: "+ min);
        }
    }
        
    //Finds out the maximum value node in the list
    public void maxNode() {
        Node current = head;
        //Initializing max to initial node data
        int max = head.data;
        if(head == null) {
            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;
                }while(current != head);
             
            System.out.println("Maximum value node in the list: "+ max);
        }
    }
                
    public static void main(String[] args) {
        MinMax cl = new MinMax();
        //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

C#

using System; 
namespace CircularLinkedList 
{                     
    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> head = null;             
             protected Node<T> tail = null;
            
            
            //This function will add the new node at the end of the list.
            public void add(T data){
                //Create new node
                Node<T> newNode = new Node<T>(data);
                //Checks if the list is empty.
                if(head == null){
                    head = newNode;
                    tail = newNode;
                    newNode.next = head;
                }else{
                    //tail will point to new node.
                    tail.next = newNode;
                    //New node will become new tail.
                    tail = newNode;
                    //Since, it is circular linked list tail will point to head.
                    tail.next = head;
                }
            }
            
            
            //Finds out the minimum value node in the list
            public void minNode() {
                Node<T> current = head;
                //Initializing min to initial node data
                T min = head.data;
                if(head == null) {
                    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;
                    }while(current != head);
                
                    Console.WriteLine("Minimum value node in the list: "+ min);
                }
            }
            
            //Finds out the maximum value node in the list
            public void maxNode() {
                Node<T> current = head;
                //Initializing max to initial node data
                T max = head.data;
                if(head == null) {
                    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;
                        }while(current != head);
                     
                    Console.WriteLine("Maximum value node in the list: "+ max);
                }
            }
        }
        
        public static void Main()
        {
            
        CreateList<int> cl = new CreateList<int>();
        //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

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 $head;
    private $tail;
    function __construct(){
        $this->head = NULL;
        $this->tail = NULL;
    }
    //This function will add the new node at the end of the list.
    function add($data){
        //Create new node
        $newNode = new Node($data);
        //Checks if the list is empty.
        if($this->head == NULL){
            //If list is empty, both head and tail would point to new node.
            $this->head = $newNode;
            $this->tail = $newNode;
            $newNode->next = $this->head;
        }
        else{
            //tail will point to new node.
            $this->tail->next = $newNode;
            //New node will become new tail.
            $this->tail = $newNode;
            //Since, it is circular linked list tail will point to head.
            $this->tail->next = $this->head;
        }
    }
    
    //Finds out the minimum value node in the list
    function minNode() {
        $current = $this->head;
        //Initializing min to initial node data
        $min = $this->head->data;
        if($this->head == NULL) {
            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;
            }while($current != $this->head);
        
            echo "Minimum value node in the list: $min <br>";
        }
    }
        
    //Finds out the maximum value node in the list
    function maxNode() {
        $current = $this->head;
        //Initializing max to initial node data
        $max = $this->head->data;
        if($this->head == NULL) {
            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;
                }while($current != $this->head);
             
            echo "Maximum value node in the list: $max <br>";
        }
    }
}
 
$cl = new 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();
?>
</body>
</html>

Output:

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf