我试图按姓氏按字母顺序将人员插入到链接列表中。我有一个很奇怪的问题。你看到它的代码工作正常,除了列表是相反的顺序。我想不出就是为什么当我使用的代码行:按字母顺序插入到LinkedList中
current != null && lastName.compareTo(current.lastName) >= 0)
插入人进入我的列表,而不是增加了100人,我加6然而,就像我上面说我能做到这一点的相反顺序没问题。这是怎么回事?
public void insert(String firstName, String lastName, String time,String show, String command,int section){
PeopleNode newNode = new PeopleNode(lastName,firstName,time,show,command,section);
size++;
PeopleNode previous = null;
PeopleNode current = head;
while(current != null && lastName.compareTo(current.lastName) <= 0){
previous = current;
current = current.next;
}
if(previous == null){
head = newNode;
}else{
previous.next = newNode;
newNode.next = current;
}
}
什么是* head *? LinkedList中的第一项? –
头显然是保存链接列表中第一个对象的变量。第一个条件(摘录在最上面)应该以降序给你一个清单。按升序写入产品的块。我不清楚你在每个案件中看到的情况。我不明白100与6.你可能包含数据吗? – AgilePro