Java教程

Java从循环链表中查找最大值和最小值节点

在此程序中,我们将创建一个循环链接列表,然后遍历该列表以找出最小和最大节点。
9->
5->
2->
7->
3
我们将维护两个变量min和max。最小值将保留最小值节点,最大值将保留最大值节点。在上面的示例中,2是最小值节点,9是最大值节点。

算法

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

程序:

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();
    }
}
输出:
Minimum value node in the list: 1
Maximum value node in the list: 20
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4