package com.qf.first;
public class ArrayStack {
//栈的大小
private int maxStack;
//数组实现栈
private int [] stack;
//表示栈顶的位置,默认没有数据时为-1
private int top=-1;
public ArrayStack(int maxStack) {
this.maxStack=maxStack;
stack=new int[maxStack];//初始化数组
}
//判断是否已经满栈
public boolean isFull() {
return this.top==this.maxStack-1;
}
//判断是否为空栈
public boolean isEmpty() {
return this.top==-1;
}
//压栈
public void push(int val) {
//是否已经满栈
if(isFull()) {
throw new RuntimeException("stack is full");
}
top++;//top=0开始
stack[top]=val;//压栈
}
//弹栈
public int pop() {
//判断是否为空
if(isEmpty()) {
throw new RuntimeException("stack is empty");
}
int value=stack[top];
top--;
return value;
}
//查看栈中所有元素
public void list() {
//判断是否为空
if(isEmpty()) {
throw new RuntimeException("stack is empty");
}
for(int i=0;i