嘿家伙我试图实现单链表的appeed方法时遇到了问题。 这里是代码:单链表添加方法
public void append (int item) {
//inserts item to the end of the list
if (head == null){
head = new LinkInt();
curr = head;
curr.elem = item;
}
else{
LinkInt temp = head;
while (temp.next != null){
temp = temp.next;}
temp.elem = item;
}
}
,这里是我的打印方法(不知道其正确的为好):
public void print() {
//outprint the array
//ie. <1, 2, |3, 4>
if ( head == null) {
System.out.print("<");
System.out.print(">");
}
else{
LinkInt temp = head;
System.out.print("<");
while (temp != null) {
if (temp == curr){
System.out.print("|" + temp.elem + ","); }
else{
System.out.print(temp.elem);
System.out.print(",");}
temp = temp.next;
}
System.out.print(">");
}
}
}
继承人的问题:
让利说上面3 - >>>我得到< | 3> ,但如果我做了后 - 5 >>>>我得到< | 5>删除我的第一个项目。
帮我个忙,请:(
为什么不储存参考尾部元素。会让事情变得更快。 – 2013-03-01 05:27:31