import java.util.LinkedList;
public class MyStack {
private LinkedList ll=new LinkedList();
int size=20;
public void push(Object o){
if(size<20)
{
ll.addFirst(o);
size--;}
}
public Object pop(){
if(size<=0) return null;
size--;
return ll.removeFirst();
}
public Object peek(){
return ll.getFirst();
}
public boolean empty(){
return ll.isEmpty();
}
public static void main(String[] args){
MyStack ms=new MyStack();
ms.push("zhangsan");
ms.push("lisi");
ms.push("wangwu");
System.out.println(ms.pop());
// System.out.println(ms.peek());
System.out.println(ms.pop());
System.out.println(ms.empty());
}
}
如果注释掉与size相关的行就是对的,但是为什么加上size的判断就错了呢?想不通,请指点