简体   繁体   English

如何为链表中的两个节点实现compareTo()

[英]How to implement compareTo() for two nodes in a linked list

I am new to Java and I am trying to implement the Comparable interface for a linked list.我是 Java 新手,我正在尝试为链表实现Comparable接口。 I have a standard linked list.我有一个标准的链表。

public class LinkedList<E extends Comparable<E>>{

    private static class Node<T> {
        private T value;
        private Node<T> next;
        private Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node<E> head;
    private int size=0;

    public boolean isEmpty() {
        return head == null;
        //or
        //return size == 0;
    }

    public void addFirst(E elem) {
        if (elem == null) {
            throw new NullPointerException();
        }
        head = new Node<E>(elem, head);
        size++;
    }

    public void addLast(E elem) {
        if (elem == null) {
            throw new NullPointerException();
        }
        if (head == null) {
            head = new Node<E>(elem, null);
        } else {
            Node<E> current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new Node<E>(elem, null);
        }
        size++;
    }

    //adding at a specific index
    public void add(E elem, int index) {
        if (elem == null) {
            throw new NullPointerException();
        }
        if (index < 0 || index > size) {
            throw new IndexOutOfBoundsException(Integer.toString(index));
        }
        if (index == 0) {
            head = new Node<E>(elem, head);
        } else {
            Node<E> p = head;
            for (int i=0; i<(index-1); i++) {
                p = p.next;
            }
            p.next = new Node<E>(elem, p.next);
        }
        size++;
    }

    public E removeFirst() {
        if (head == null) {
            throw new NullPointerException();
        }
        E saved = head.value;
        head = head.next;
        size--;
        return saved;
    }

    public E removeLast() {
        if (head == null) {
            throw new NullPointerException();
        }
        E saved;
        if (head.next == null) {
            saved = head.value;
            head = null;
        } else {
            Node<E> p = head;
            while (p.next.next != null) {
                p = p.next;
            }
            saved = p.next.value;
            p.next = null;
        }
        size--;
        return saved;
    }

    public int size() {
        return size;
    }

    public String toString() {
        String str = "[";
        Node<E> p = head;
        while (p!=null) {
            if (p != head) {
                str += ", ";
            }
            str += p.value;
            p = p.next;
        }
        str += "]";
        return str;
    }

    public int compareTo(Node<E> other){
      return Integer.compare(this.value,other.value);
    }


}

Currently I am assuming the contents of the list are of type int.目前我假设列表的内容是 int 类型。 When I try to compile, I get the error as当我尝试编译时,出现以下错误

LinkedList.java:144: error: cannot find symbol
      return Integer.compare(this.value,other.value);
                                 ^

Based on what I can understand, it's because I am trying to compare Objects of type Node and not linkedList, therefore "this" is referring linkedList.根据我的理解,这是因为我试图比较 Node 类型的对象而不是linkedList,因此“this”指的是linkedList。 I am not sure how I can change my code to be able to compare two nodes.我不确定如何更改代码以比较两个节点。 Any help or suggestions is appreciated.任何帮助或建议表示赞赏。

Edit: Is my general methodology incorrect or just my implementation?编辑:我的一般方法不正确还是只是我的实现? Is there another way I should implement compareTo ?还有另一种方法我应该实现compareTo吗? eg Add it as a method in the class node?例如将其添加为类节点中的方法?

value is a field in Node and this in your current compareTo method refers to a the linked list object. valueNode一个字段,并且this在您当前的compareTo方法中是指一个链表对象。

You could make the Node class to implement Comparable as below:您可以使Node类实现Comparable如下:

public static class Node<T> implements Comparable<Node<T>> {
    // rest of the code
    @Override
    public int compareTo(Node<T> other) {
        // this.value is accessible.
    }
}

Now in your LinkedList class write another compareTo method (or any other name as this method is not related to Comparable.compareTo ) which would invoke the above.现在在您的LinkedList类中编写另一个compareTo方法(或任何其他名称,因为此方法与Comparable.compareTo无关)将调用上述方法。

public int compareTo(Node<E> other) {
    return this.head.compareTo(other);
}

NOTE: In your current code, you haven't actually "implemented" the Comparable interface.注意:在您当前的代码中,您实际上并没有“实现” Comparable接口。 You have only mentioned that the generic type E is a Comparable type.您刚刚提到泛型类型EComparable类型。

You didn't understand this <E extends Comparable<E>> ,There are two solutions你没看懂这个<E extends Comparable<E>> ,有两种解决方法

1. 1.

public class LinkedList<E extends Comparable<E>>{
//public class LinkedList<E extends Integer>{

    private static class Node<T> {
        private T value;
        private Node<T> next;
        private Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node<E> head;
    private int size=0;

    public int compareTo(Node<E> other){
      return this.head.value.compareTo(other.value);
      //return Integer.compare(this.head.value, other.value);
    }


}
//public class LinkedList<E extends Comparable<E>>{
public class LinkedList<E extends Integer>{

    private static class Node<T> {
        private T value;
        private Node<T> next;
        private Node(T value, Node<T> next) {
            this.value = value;
            this.next = next;
        }
    }

    private Node<E> head;
    private int size=0;

    public int compareTo(Node<E> other){
      //return this.head.value.compareTo(other.value);
      return Integer.compare(this.head.value, other.value);
    }

}

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

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