Using java write two methods min and max to find maximum value and minimum value in linked list but the input list is array of integers

user2495 :
  • I want to write two methods min and max to find maximum value of a node and minimum value of a node in linked list.
  • for example if a variable called abc stores { 1, 78, -9, 42 , 0, 14} then abc.min() should return -9 and abc.max() should return 78.
  • If the list is empty it should return -1. Print the returned value.

    Please help me in this how I can compute max and min values in a linked list by inputting array of integers

    ```package demo;
    
      public class MaximumMinimum {
    
       class Node{  
            int data;  
            Node next;  
    
        public Node(int data) {  
            this.data = data;  
            this.next = null;  
        }  
    }  
    
    //Represent the head and tail of the singly linked list 
        public Node head = null;  
        public Node tail = null;  
    
       //addNode() will add a new node to the list  
       public void addNode(int data) {  
           //Create a new node  
           Node newNode = new Node(data);  
    
          //Checks if the list is empty  
        if(head == null) {  
            //If list is empty, both head and tail will point to new node  
            head = newNode;  
            tail = newNode;  
        }  
        else {  
            //newNode will be added after tail such that tail's next will point to newNode  
            tail.next = newNode;  
            //newNode will become new tail of the list  
            tail = newNode;  
        }  
    }  
    
    //minNode() will find out the minimum value node in the list  
    public void minNode() {  
        Node current = head;  
        int min;  
    
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
            //Initializing min with head node data  
            min = head.data;  
    
            while(current != null){  
                 //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);  
        }  
    }  
    
    //maxNode() will find out the maximum value node in the list  
    public void maxNode() {  
        Node current = head;  
        int max;  
    
        if(head == null) {  
            System.out.println("List is empty");  
        }  
        else {  
            //Initializing max with head node data  
            max = head.data;  
    
            while(current != null){  
                 //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) {
        MaximumMinimum sList = new MaximumMinimum(); 
    
        //Adds data to the list  
        sList.addNode(5);  
        sList.addNode(8);  
        sList.addNode(1);  
        sList.addNode(6);  
    
        //Display the minimum value node in the list  
        sList.minNode();  
    
        *//Display the maximum value node in the list *
        sList.maxNode();  
        }  
      }  ```
    
Aashish Pawar :

Code you given is executing in correct manner.

You can also use

MaximumMinimum sList = new MaximumMinimum(); 
List<Integer> list = new ArrayList<>();
Node head = sList.head;
while(head != null){
    list.add(head.data);
    head= head.next;
}
//no recommended if you want to design your own method    
System.out.println(list);
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));

For inputting array of integers to

public void stores(int[] array)
{
    for(int element:array)
    {
        this.addNode(element);
    }
}

then if you run in main

 int[] elements = {1, 78, -9, 42 , 0, 14};
    sList.stores(elements);
    sList.maxNode(); //78
    sList.minNode();//-9

Also you can use Arrays.stream(array_name).forEach(e->sList.add(e)) if You want to do it in java 8 way.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317144&siteId=1