1
我有我的冒泡的麻煩。它在結束之前循環一次。它接受一個輸入文件,其中包含各種名稱和年齡並對它們進行分類。到目前爲止,它打印出:冒泡與LinkedList的
Stella is 11 years old.
Fred is 88 years old.
George is 22 years old.
Violet is 33 years old.
Rose is 77 years old.
Bob is 66 years old.
Lena is 55 years old.
Billy is 44 years old.
Nellie is 99 years old.
時,我想它打印出來:
Stella is 11 years old.
George is 22 years old.
Violet is 33 years old.
Billy is 44 years old.
Lena is 55 years old.
Bob is 66 years old.
Rose is 77 years old.
Fred is 88 years old.
Nellie is 99 years old.
當前代碼:
public void bubbleSort()
{
PersonNode previous = head;
PersonNode current = head.getNext();
boolean swap = true;
int j = 0;
while(swap)
{
swap = false;
while(previous != null)
{
if(previous.getNext() != null && previous.getAge() > previous.getNext().getAge())
{
String tempName = previous.getName();
Integer tempAge = previous.getAge();
current = previous.getNext();
previous.setName(current.getName());
previous.setAge(current.getAge());
current.setName(tempName);
current.setAge(tempAge);
swap = true;
}
previous = previous.getNext();
}
}
任何幫助表示讚賞。
感謝,這樣一個簡單的錯誤。 – Jonahlol 2014-11-05 20:47:50
很樂意幫忙,請接受的答案,如果它固定您的問題,謝謝。 – ryanpattison 2014-11-05 20:57:54