所以我想做的是使用有序链表实现一个优先级队列。我觉得我已经很接近了,但我似乎无法解决插入函数中 while 语句中的错误。这是我到目前为止所拥有的:
class OrderedLinkedListMaxPQ<Item> {
private class PQNode {
Object data;
PQNode next;
public PQNode(Object value) {
data = value;
next = null;
}
}
PQNode head;
public void PriorityQueue() {
this.head = null;
}
public boolean isEmpty() { return this.head == null; }
public void insert(Object item) {
PQNode prev = null;
PQNode current = this.head;
while (current != null && current.data >= item.data) {
prev = current;
current = current.next;
}
PQNode temp = new PQNode(item);
if (prev == null) {
temp.next = this.head;
this.head = temp;
} else {
temp.next = current;
prev.next = temp;
}
}
public Object delete() {
Object temp = this.head.data;
this.head = this.head.next;
return temp;
}
public static void main(String[] args) {
OrderedLinkedListMaxPQ<Integer> pq = new OrderedLinkedListMaxPQ<Integer>();
pq.insert(7);
pq.insert(6);
pq.insert(3);
pq.insert(2);
while (!pq.isEmpty())
StdOut.println(pq.delete());
}
}