[蟲?] Stack 的code 有錯
剛剛在看Cracking the Coding Interview
發現他的程式有一個小錯
他是這樣寫的
class Stack{
Node top;
Object pop(){
if (top != null){
Node item = top.data;
top = top.next;
^^^^^^^^^
return item;
}
return null;
}
void push(Object item){
Node t = new Node(item);
t.next = top;
top = t;
}
Object peek(){
return top.data;
}
}
........................
在 pop()這個方法裡,top.data 依照書中class Node裡的data型態是int
所以Node item 不應該等於top.data
應該要改成 Node item = top;
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 135.0.163.188
※ 文章網址: https://www.ptt.cc/bbs/java/M.1428864138.A.1CF.html
※ 編輯: yule1224 (135.0.163.188), 04/13/2015 02:43:36
推
04/13 03:11, , 1F
04/13 03:11, 1F
java 近期熱門文章
PTT數位生活區 即時熱門文章