Java用LinkedList实现栈

来源:互联网 发布:mac os yosemite 镜像 编辑:程序博客网 时间:2024/04/28 12:46
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());}}

输出结果为

threetwotwofalse


原创粉丝点击