嘿,我有一个使用 while 循环的程序,但我真的很困惑为什么它变成了无限循环
这是我的代码
打印无效
public void print() {
DoublyLinkedListNode current = first;
while (current != null) {
current.displayInfo();
current = current.next;
}//end while
}//end print
public DoublyLinkedListNode partition(DoublyLinkedList list,
DoublyLinkedListNode first, DoublyLinkedListNode last) {
DoublyLinkedListNode smallIndex = first;
DoublyLinkedListNode index = smallIndex.next;
DoublyLinkedListNode temp = new DoublyLinkedListNode();
double pivot = first.ipk;
while (index != temp.next) {
if ((index.ipk) < pivot) {
smallIndex = smallIndex.next;
temp.ipk = index.ipk;
index.ipk = smallIndex.ipk;
smallIndex.ipk = temp.ipk;
}
index = index.next;
}
temp.ipk = first.ipk;
first.ipk = smallIndex.ipk;
smallIndex.ipk = temp.ipk;
System.out.println("The list in partition is: ");
list.print();
System.out.print("\n");
return first;
}
public void recQuickSort(DoublyLinkedList list, DoublyLinkedListNode first,
DoublyLinkedListNode last) {
while (first != last) {
DoublyLinkedListNode pivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation.back);
recQuickSort(list, pivotLocation.next, last);
}
}
主要的
public static void main(String[] args) {
DoublyLinkedList d = new DoublyLinkedList();
d.insertNode("Apep", "123", 3.5);
d.insertNode("Alex", "121", 3.2);
d.insertNode("Kujul", "124", 3.1);
d.insertNode("Fahmi", "125", 3.7);
d.print();
d.quickSort(d);
d.print();
}
所以从这些代码中会有无限循环输出,我不知道我的程序中哪个是无限循环的。谢谢。