我试图实现一个有插入,删除,并检查整数是否存在方法的排序链接列表,我目前无法理解为什么我的插入方法工作不正确。它将整数插入链表中,但它们向后排序,我尝试过移动条件,但它不能按照它的方式工作。插入排序的链接列表
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