Java教程

Java从双向链表中查找最大值和最小值节点

在此程序中,我们将创建双向链接列表,然后遍历该列表以找出最小和最大节点。
从双向链表中找到最大值和最小值节点的Java程序
我们将维护两个变量min和max。最小值将保留最小值节点,最大值将保留最大值节点。在上面的示例中,1将是最小值节点,9将是最大值节点。

算法

定义一个代表列表中节点的Node类。它将具有三个属性: 数据,前一个将指向上一个节点,下一个将指向下一个节点。 定义另一个用于创建双向链表的类,它有两个节点: head和tail。最初,头和尾将指向null。 minimumNode()将打印出最小值节点: 定义变量min并使用head的数据进行初始化。 电流将指向头部。 通过比较每个节点的数据和最小值来遍历列表。 如果min>当前数据,则min将保存当前数据。 在列表的末尾,变量min将保存最小值节点。 打印最小值。
a.maximumNode()将打印出最大值节点:
定义变量max并使用head的数据进行初始化。 电流将指向头部。 通过比较每个节点的数据与最大值来遍历列表。 如果max <当前数据,则max将保存当前数据。< span> 在列表的末尾,变量max将保存最大值节点。 打印最大值。

程序:

public class MinMax {
    //Represent a node of the doubly linked list
    class Node{
        int data;
        Node previous;
        Node next;
        public Node(int data) {
            this.data = data;
        }
    }
    //Represent the head and tail of the doubly linked list
    Node head, tail = null;
    //addNode() will add a node to the list
    public void addNode(int data) {
        //Create a new node
        Node newNode = new Node(data);
        //if list is empty
        if(head == null) {
            //Both head and tail will point to newNode
            head = tail = newNode;
            //head's previous will point to null
            head.previous = null;
            //tail's next will point to null, as it is the last node of the list
            tail.next = null;
        }
        else {
            //newNode will be added after tail such that tail's next will point to newNode
            tail.next = newNode;
            //newNode's previous will point to tail
            newNode.previous = tail;
            //newNode will become new tail
            tail = newNode;
            //As it is last node, tail's next will point to null
            tail.next = null;
        }
    }
    //MinimumNode() will find out minimum value node in the list
    public int minimumNode() {
        //Node current will point to head
        Node current = head;
        int min;
        //Checks if list is empty
        if(head == null) {
            System.out.println("List is empty");
            return 0;
        }
        else {
            //Initially, min will store the value of head's data
            min = head.data;
            while(current != null) {
                //if the value of min is greater than the current's data
                //Then, replace the value of min with current node's data
                if(min > current.data)
                    min = current.data;
                current = current.next;
            }
        }
        return min;
    }
    //MaximumNode() will find out maximum value node in the list
    public int maximumNode() {
        //Node current will point to head
        Node current = head;
        int max;
        //Checks if list is empty
        if(head == null) {
            System.out.println("List is empty");
            return 0;
        }
        else {
            //Initially, max will store the value of head's data
            max = head.data;
            //if value of max is lesser than current's data
            //Then, replace value of max with current nodes data
            while(current != null) {
                if(current.data > max)
                    max = current.data;
                current = current.next;
            }
        }
        return max;
    }
    public static void main(String[] args) {
        MinMax dList = new MinMax();
        //Add nodes to the list
        dList.addNode(5);
        dList.addNode(7);
        dList.addNode(9);
        dList.addNode(1);
        dList.addNode(2);
        //Prints the minimum value node in the list
        System.out.println("Minimum value node in the list: "+ dList.minimumNode());
        //Prints the maximum value node in the list
        System.out.println("Maximum value node in the list: "+ dList.maximumNode());
    }
}
输出:
Minimum value node in the list: 1
Maximum value node in the list: 9
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4