Java用LinkedList实现栈
2012-07-27 17:14 javaspring 阅读(246) 评论(0) 编辑 收藏 举报import java.util.LinkedList; public class MyStack { private LinkedList ll=new LinkedList(); public void push(Object o) { ll.addFirst(o); } public Object pop() { if(ll.isEmpty()) { System.out.println("栈为空,不能出栈!"); return null; } 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("one"); ms.push("two"); ms.push("three"); System.out.println(ms.pop()); System.out.println(ms.peek()); System.out.println(ms.pop()); System.out.println(ms.empty()); } }
输出结果为
three two two false