嘿傢伙我試圖實現單鏈表的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