2
在將第三個值插入隊列後,我得到一個無限循環。鏈接列表上的無限循環優先級隊列
代碼插入方法:
public boolean insert(E object) {
Node<E> Nody = new Node(object);
Node<E> current = head;
Node<E> previous = null;
if (isEmpty())
head = Nody;
while (current != null) {
if (((Comparable<E>) object).compareTo(current.data) >= 0) {
previous = current;
current = current.next;
} else {
if (((Comparable<E>) object).compareTo(current.data) < 0) {
previous = Nody;
Nody = current;
}
if (previous == null && ((Comparable<E>) object).compareTo(current.data) < 0) {
Nody.next = head;
head = Nody;
}
}
}
currentSize++;
return false;
}
這裏是我使用的驅動程序和地方,我得到了無限循環:
public P2Driver() {
list = new OrderedListPriorityQueue<TestInteger>();
randomInteger = new Random(8);
randomPriority = new Random(8);
print("");
print("/////////////////////////////////////////////////////////");
print("/// BEGIN TESTING LIST IMPLEMENTATION");
print("/////////////////////////////////////////////////////////");
print("");
// Test insert
printCurrentSize();
print("--- BEGIN TESTING INSERT() --- ");
print("Checking if the list is empty BEFORE insertion...");
start = System.currentTimeMillis();
for (int i = 0; i < MAX_SIZE; i++) {
print(i + ". Is it empty? [ " + list.isEmpty() + " ]");
list.insert(new TestInteger(randomInteger.nextInt(10)));
} //GETS STUCK HERE<<<<<<<<<<<<<<<<<
輸出:
/////////////////////////////////////////////////////////
/// BEGIN TESTING LIST IMPLEMENTATION
/////////////////////////////////////////////////////////
Current Size: 0
--- BEGIN TESTING INSERT() ---
Checking if the list is empty BEFORE insertion...
0. Is it empty? [ true ]
1. Is it empty? [ false ]
2. Is it empty? [ false ]
由於是不存在的代碼,這意味着驅動程序卡住了。我不知道我做錯了什麼。
你真的應該在類聲明中指定類型參數,比如'E extends Comparable' –
oldrinb