我試圖實現一個有插入,刪除,並檢查整數是否存在方法的排序鏈接列表,我目前無法理解爲什麼我的插入方法工作不正確。它將整數插入鏈表中,但它們向後排序,我嘗試過移動條件,但它不能按照它的方式工作。插入排序的鏈接列表
void insert(int x){
LinkedList newLink = new LinkedList(x);
if (front == null) {
front = newLink;
} else if (x > front.x){
newLink.next = front;
front = newLink;
} else {
LinkedList current = front.next;
LinkedList before = front;
while (current != null){
if (x < front.x) {
before = current;
current = current.next;
}
newLink.next = before.next;
before.next = newLink;
}
}
}
嗯,如果'x'大於你當前的頭,然後你*預計*它到你的名單。這就是使列表按降序排列的原因。 – 5gon12eder 2014-12-07 21:22:55