刚刚在看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;