简体   繁体   English

如何找到链表的最大/最小元素

[英]How to find the max/min element of linked list

I have a doubly linked list in my case. 在我的情况下,我有一个双链表。 And I want to find the max and min element. 我想找到最大和最小元素。 So I want to use the Collections to find it. 因此,我想使用收藏集来查找它。 Here is my code below for Node first: 这是我下面的节点优先代码:

public class Node<T> {
    Node<T> prev;
    Node<T> next;
    T data;

    public Node(T _data)
    {
        data = _data;
        prev = null;
        next = null;
    }

    public Node(T _data, Node<T> _prev, Node<T> _next)
    {
        data = _data;
        prev = _prev;
        next = _next;
    }

    T getData()
    {
        return data;
    }

    public void setNext(Node<T> _next)
    {
        next = _next;
    }

    public void setPrev(Node<T> _prev)
    {
        prev = _prev;
    }

    public Node<T> getNext()
    {
        return next;
    }

    public Node<T> getPrev()
    {
        return prev;
    }
}

And here is my Doubly Linked List class: 这是我的双链表类:

public class DoublyLinkedList<T> {
    private Node<T> head;
    private Node<T> tail;
    int listCount = 0;

    public void traverseF()
    {
        Node<T> temp = head;

        while(temp != null)
        {
            System.out.print(temp.getData() + " ");
            temp = temp.getNext();
        }
    }

    public void traverseB()
    {
        Node<T> temp = tail;

        while(temp != null)
        {
            System.out.print(temp.getData() + " ");
            temp = temp.getPrev();
        }
    }

    public void insertFirst(T data)
    {
        Node<T> temp = new Node<T>(data);
        if(head == null)
        {
            head = temp;
            tail = temp;
            temp.setNext(null);
            temp.setPrev(null);
        }
        else
        {
            temp.setNext(head);
            head.setPrev(temp);
            head = temp;
        }
    }

}

So, my main code is: 所以,我的主要代码是:

import java.util.Collections;


public class glavna {

    public static void main(String[] args) {
        DoublyLinkedList<Integer> DLL = new DoublyLinkedList<Integer>();

        DLL.insertFirst(32);
        DLL.insertFirst(22);
        DLL.insertFirst(55);
        DLL.insertFirst(10);

        DLL.traverseF();

        Integer max = Collections.max(DLL);

    }
}

How exactly do I call the Collections.max or Collections.min method? 我究竟该如何调用Collections.max或Collections.min方法? Isn't the list only necessary to find the max/min elements? 列表不是只需要找到最大/最小元素?

public T getMin()
{
    Node<T> temp = head;
    T min = head.getData();
    while(temp.getNext() != null)
    {
        if(temp.getData() < min) // error
        {
            //min = temp.getData();
        }
    }
}

To implement getMin with generics you need to be able to compare them. 要使用泛型实现getMin ,您需要能够对其进行比较。 You can, for instance, provide a custom Comparator to your method: 例如,您可以为您的方法提供自定义比较器

public T getMin(Comparator<? super T> comparator) {
    Node<T> temp = head.getNext();
    T min = head.getData();
    while(temp != null) {
        T candidateValue = temp.getData();
        if (comparator.compare(candidateValue, min) < 0) { // equivalent to candidate < min
            min = candidateValue;
        }
        temp = temp.getNext();
    }
    return min;
}

Then, calling your method for Integer : 然后,为Integer调用方法:

getMin(new Comparator<Integer>() {
    @Override
    public int compare(Integer arg0, Integer arg1) {
        return arg0.compareTo(arg1);
    }
 });

Another approach is to make your list only keep Comparable items : 另一种方法是使您的列表仅保留可比较项:

public class DoublyLinkedList<T extends Comparable<? super T>> {

and then have your getMin() method use compareTo method : 然后让您的getMin()方法使用compareTo方法:

public T getMin() {
    Node<T> temp = head.getNext();
    T min = head.getData();
    while(temp != null) {
        T candidateValue = temp.getData();
        if (candidateValue.compareTo(min) < 0) { // equivalent to candidate < min
            min = candidateValue;
        }
        temp = temp.getNext();
    }
    return min;
}

Second approach is less verbose, as Integer is Comparable (ie implements Comparable for you already), so you won't need to change any other code. 第二种方法不太冗长,因为IntegerComparable (即已经为您实现Comparable),因此您无需更改任何其他代码。

您列出的不是集合,因此您无法将其与集合一起使用。

The Collections.max method expects an argument which implements Collection . Collections.max方法需要一个实现Collection的参数。 The easiest way would probably be to extend AbstractCollection and add these methods: 最简单的方法可能是扩展AbstractCollection并添加以下方法:

@Override
public Iterator<T> iterator() {
    return new Iterator<T>() {
        private Node<T> node = head;
        @Override
        public boolean hasNext() {
            return node != null;
        }

        @Override
        public T next() {
            T next = node.data;
            node = node.getNext();
            return next;
        }
    };
}

@Override
public int size() {
    int size = 0;
    Node<T> node = head;
    while (node != null) {
        size++;
        node = node.getNext();
    }
    return size;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM