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

标签 java arrays linked-list max min

  • 我想编写两个方法 min 和 max 来查找链表中节点的最大值和最小值。
  • 例如,如果名为 abc 的变量存储 { 1, 78, -9, 42 , 0, 14},则 abc.min() 应返回 -9,abc.max() 应返回 78。
  • 如果列表为空,则应返回 -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();  
        }  
      }  ```
    

最佳答案

您给出的代码正在以正确的方式执行。

您还可以使用

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

您也可以使用Arrays.stream(array_name).forEach(e->sList.add(e))如果你想用 java 8 的方式来做。

关于java - 使用java编写两个方法min和max来查找链表中的最大值和最小值,但输入列表是整数数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60750598/

相关文章:

通过结构数组中的函数指针调用函数

javascript - 使用 DOM 和封装方法,使用 Javascript OOP 传递参数。多少算太多?

c - 保留结构而不进行动态分配

java - 'thread-safe' 在 Java 中的集合或数组的上下文中意味着什么?

java - Hudson CI 和 FindBugs/静态分析插件的问题

arrays - Matlab Accumarray 用于 3D 矩阵

C - strsep 分割字符串

java - 如何计算括号

Java:在数组中使用 "08"和 "09"

algorithm - 在单链表时间复杂度中查找节点