使用java编写两个方法min和max在链表中查找最大值和最小值,但输入列表是整数数组

Posted

技术标签:

【中文标题】使用java编写两个方法min和max在链表中查找最大值和最小值,但输入列表是整数数组【英文标题】: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 【发布时间】:2020-06-30 04:47:38 【问题描述】: 我想写两个方法 min 和 max 来查找链表中节点的最大值和节点的最小值。 例如,如果一个名为 abc 的变量存储 1, 78, -9, 42 , 0, 14 那么 abc.min() 应该返回 -9 而 abc.max() 应该返回 78。 em>

如果列表为空,它应该返回 -1。打印返回值。

请帮助我了解如何通过输入整数数组来计算链表中的最大值和最小值

```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();  
      
    ```

【问题讨论】:

您需要手工实现吗?或者你可以使用比较器? 我想要上述问题的完整可执行代码。 只需阅读可比较的内容或收集并排序您的元素并获得第一 - 最小,最后 - 最大 是的,比较器也可以,但是链表的输入是一个整数数组,即变量 int[ ] abc = 1, 78, -9, 42 , 0, 14 Arrays.asList(这里是你的数组) = List. 【参考方案1】:

您为minNode() 函数添加了一个大于号,为maxNode() 设置了一个小于号。

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  


            // Your error was here:

             min = Math.min(min, current.data); // You can simplify updating min to this.




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

maxNode() 变成了这样:

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  


             // Your error was here:

             max = Math.max(max, current.data); // And updating max becomes this.




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

【讨论】:

【参考方案2】:

您提供的代码正在以正确的方式执行。

你也可以使用

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));

用于输入整数数组到

public void stores(int[] array)

    for(int element:array)
    
        this.addNode(element);
    

那么如果你在 main 中运行

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

如果您想以 java 8 方式进行操作,也可以使用 Arrays.stream(array_name).forEach(e-&gt;sList.add(e))

【讨论】:

非常感谢 Aashish Pawar!代码运行完美!! 欢迎您随时提问,如果您喜欢我的方法,请点赞

以上是关于使用java编写两个方法min和max在链表中查找最大值和最小值,但输入列表是整数数组的主要内容,如果未能解决你的问题,请参考以下文章

使用 Hare and Tortoise 方法在链表中检测循环

Java 实例 – 链表元素查找

数据结构:单向链表系列5--在链表中查找元素

Python 3 - 在链表中使用递归

为啥在链表中查找循环时将指针增加 2,为啥不增加 3、4、5?

c_cpp 在链表中查找循环的长度