读书人

关于链表的一个有关问题

发布时间: 2011-12-29 22:09:38 作者: rapoo

关于链表的一个问题
程序有点长,但很简单,就是一个普通的链表。
请问程序中的
end.next = newEnd;
end = newEnd;
这两条语句有什么用处?第二条没有看懂~~


public class LinkedList {

public LinkedList() {
}

public LinkedList(Object[] items){
if (items != null){
for (int i = 0; i < items.length; i++){
addItem(items[i]);
}
current = start;
}
}

public void addItem(Object item){
ListItem newEnd = new ListItem(item);
if (start == null){
start = end = newEnd;
} else {
end.next = newEnd;
end = newEnd;
}
}

public Object getFirst(){
current = start;
return start == null ? null : start.item;
}

public Object getNext(){
if (current != null){
current = current.next;
}
return current == null ? null : current.item;
}

private ListItem start = null;
private ListItem end = null;
private ListItem current = null;
private class ListItem {
public ListItem(Object item){
this.item = item;
next = null;
}

public String toString(){
return "ListItem " + item;
}

ListItem next;
Object item;
}
}

[解决办法]
将结尾的end连到newEnd上
再将newEnd指定为结尾
[解决办法]
就是楼上的意思
链表这东西用手画画图就很好理解了~
------解决方案--------------------


增加链表长多。

始终让end指向链表的末尾,使得在此增加长度时也这样操作.

读书人网 >J2SE开发

热点推荐