作者yule1224 (支持自经区服贸货贸)
看板java
标题[虫?] Stack 的code 有错
时间Mon Apr 13 02:42:14 2015
刚刚在看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://webptt.com/cn.aspx?n=bbs/java/M.1428864138.A.1CF.html
※ 编辑: yule1224 (135.0.163.188), 04/13/2015 02:43:36
1F:推 pttworld: 未找资料,但应该就是你所讲的= top; 推个。 04/13 03:11