package com.dataconstrcure; public interface Stack { /** * 判断栈是否为空 * @return */ boolean isEmpty(); /** * 返回栈顶元素,并不出栈 * @return */ Object peek(); /** * 入栈 * @param target * */ void push(Object o); /** * 出栈 * @return E */ Object pop(); } package com.dataconstrcure; /** * Stack的基于单链表的实现 */ public class LinkedListStack implements Stack { private static class Node{ Object o ; Node next; public Node(Object o){ this.o=o; } public Node(){ } } private Node top = null; public boolean isEmpty() { return top == null; } public Object peek() { if(top==null) throw new java.util.EmptyStackException(); return top.o; } public Object pop() { if(top==null) throw new java.util.EmptyStackException(); Object o = top.o; top = top.next; return o; } public void push(Object o) { Node temp = new Node(o); temp.next = top; top = temp; } }