1、源码
ArrayStack类
package com.study.stack;public class ArrayStack {private int maxsize;private int[] stack;private int top = -1;public ArrayStack(int maxsize) {this.maxsize = maxsize;stack = new int[this.maxsize];}//栈满public boolean isFull(){return top == maxsize-1;}//栈空public boolean isEmpty(){return top == -1;}//入栈public void push(int value){if (isFull()){System.out.println("栈满");}top++;stack[top] = value;}//出栈public void pop(){if (isEmpty()){throw new RuntimeException("栈空");}int value = stack[top];top--;System.out.println(value);}//显示栈public void show(){if (isEmpty()){System.out.println("栈空");}for (int i=top;i>=0;i--){System.out.println(stack[i]);}}}
ArrayStackDemo测试类
package com.study.stack;
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
System.out.println("===========");
stack.pop();
System.out.println("==============");
stack.show();
}
}
测试结果
