/** * 栈 (LIFO) * * @author *** * */ class DS_Stack { private int maxSize; private long[] stackArray; private int top; /** * 初始化栈数组 * * @param s */ public DS_Stack(int s) { maxSize = s; stackArray = new long[maxSize]; top = -1; } /** * 向栈中添加数据 * * @param j */ public void push(long j) { stackArray[++top] = j; } /** * 从栈中拿处数据 * * @return */ public long pop() { return stackArray[top--]; } /** * 查看栈头 * * @return */ public long peek() { return stackArray[top]; } /** * 判断栈是否为空 * * @return */ public boolean isEmpty() { return (top == -1); } /** * 判断栈是否已满 * * @return */ public boolean isFull() { return (top == maxSize - 1); } public static void main(String[] args) { DS_Stack stack = new DS_Stack(10); stack.push(20); stack.push(40); stack.push(60); stack.push(80); while (!stack.isEmpty()) { long value = stack.pop(); System.out.print(value); System.out.print(" "); } System.out.println(""); } }