使用嵌套在另一个扩展 Comparable 的 class 中的 class 覆盖 compareTo 方法

Overriding the compareTo method with a class that is nested inside another class which extends Comparable

我的任务是在 Java 中编写自己的 PriorityQueue class。它基于链表。引用说明:

节点中存储的数据类型应该是可比较的通用类型。那是为 class 声明写的: public class PriorityQueue (E extends Comparable)) -> 注意:花括号的意思是 <>,无论我在 <> 之间写什么都是消失...

我将使用 PriorityQueue 编写另外两个 classes,一个是 patient 类型,另一个是 waitingRoom。这是 compareTo 方法发挥作用的地方,因为我将两个 classes 分类到各自的 PriorityQueues 中。

我一直在 PriorityQueue class 本身内部定义 ListNode class,所以我在 class 中有一个 class。现在问题来了:

我要去哪里 implement/Override 从 Comparable 继承的 compareTo 方法?

无法在 PriorityQueue 中实现 class 因为 compareTo 只能接受一个参数。然而,这似乎是它应该去的地方,因为这是实际的 class 扩展 Comparable。

如果我在 ListNode class 中实现它,好吧,我不知道我会怎么做。我是否将 ListNode 变成一个接口?抽象类?

下面是我写的比较新手的代码,感谢帮助

    package hostpitalQueue;

import java.util.AbstractList;

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

    private ListNode front;

    public PriorityQueue() {
        front = null;
    }
    public PriorityQueue(ListNode n1) {
        front = n1;
    }



    //method for addingNode to beginning, 
    //perhaps overload method for next nodes?
    public void addNode(ListNode n1) {

        if(front == null) {
            front = n1;
        }else {
                //need to find last node and add n1 to it
                ListNode lastNode = findLastNode(n1);
                lastNode.addNode(n1);
            }
    }



    //need to compare, remember, this is a priorityqueue
    public ListNode findLastNode(ListNode n) {
        //compare the data of both
        //compare to front

        ListNode n1 = front;
        int i = n1.compareTo(n);
        //only do something here if n is higher priority
        if(i > 0) {
            E frontData = n1.data;
            E nodesData = n.data;
            ListNode holder = n1;
            front = n;
            n.next = holder;
            holder.previous = n;
        }else if(n1.next == null) {
            n1.next = n;
            n.previous = n1;
        }
        else {

            while(front.next != null) {
                n1 = front.next;
                //is n1 a higher priority?
                Integer ii = n1.compareTo(n);
                if(ii > 0) {
                    //this means that we should return the previous node, to insert
                    //before this one
                    return n1.previous;
                }
            }
        }
        return n1;
    }


    public class ListNode  {
        //contains a left and a right, as well as a data field
        public E data;
        public ListNode previous,next;


        //construct
        public ListNode() {
            data = null;
            previous = null;
            next = null;
        }

        //previous to next
        public ListNode(E data) {
            this.data = data;
            previous = null;
            next = null;
        }

        public ListNode(E data,ListNode n1) {
            this.data = data;
            previous = n1;
            next = null;
        }

        public ListNode(E data,ListNode n1,ListNode n2) {
            this.data = data;
            previous = n1;
            next = n2;
        }

        public void addNode(ListNode n1) {
            //gotta check if my next is null
            ListNode holder = null;
            if(this.next != null) {
                holder = this.next;
            }
            this.next = n1;
            n1.previous = this;
            n1.next = holder;
        }
        public int compareTo(ListNode n1) {
            return 0;
        }

        public void printMe() {
            System.out.println(this.data);
        }

    }







}

如果您想比较两个 ListNode,如行中所示:

n1.compareTo(n)

您需要让它实现 Comparable:

public class ListNode implements Comparable<ListNode> {

并实现 compareTo 方法,如下所示:

return this.data.compareTo(that.data);

(因为您知道 dataComparable)。但是您必须处理空数据的情况。


请注意,您还应该在顶层 class 将类型变量声明为:

<E extends Comparable<? super E>>

将用作 PriorityQueue 元素类型的每个 class 都必须实现 Comparable 接口和 compareTo 方法。

请注意,由于您的 ListNode class 实现了 compareTo 方法,您可以让它实现 Comparable<ListNode>:

public class ListNode implements Comparable<ListNode>  {

...

    @Override
    public int compareTo(ListNode n1) {
        return data.compareTo(n1.data);
    }

请注意,由于您 ListNode classe 不依赖于 PriorityQueue 的实例,因此您可以做到 static;在这种情况下,您将不得不声明一个通用参数:

public static class ListNode<T extends Comparable<T>>
        implements Comparable<ListNode<T>>  {
    //contains a left and a right, as well as a data field
    public T data;
    public ListNode<T> previous,next;


    //construct
    public ListNode() {
        data = null;
        previous = null;
        next = null;
    }

    //previous to next
    public ListNode(T data) {
        this.data = data;
        previous = null;
        next = null;
    }

    public ListNode(T data,ListNode<T> n1) {
        this.data = data;
        previous = n1;
        next = null;
    }

    public ListNode(T data,ListNode<T> n1,ListNode<T> n2) {
        this.data = data;
        previous = n1;
        next = n2;
    }

    public void addNode(ListNode<T> n1) {
        //gotta check if my next is null
        ListNode<T> holder = null;
        if(this.next != null) {
            holder = this.next;
        }
        this.next = n1;
        n1.previous = this;
        n1.next = holder;
    }

    @Override
    public int compareTo(ListNode<T> n1) {
        return data.compareTo(n1.data);
    }

    public void printMe() {
        System.out.println(this.data);
    }
}

class ListNode 有字段,所以你不能把它改成接口(你到底为什么要那样做?)

您也许应该问问自己是否真的需要双向链表,单向链表是否不够用。

你的问题不清楚你是否需要实现链表或者你是否可以使用 class LinkedList 在这种情况下你不需要 ListNode class: PriorityQueue 只会封装一个 LinkedList<E>.