public Stack{
int[] data;
int maxSize;
int top;
public Stack(int maxSize){
this.maxSize = maxSize; //数组长度
data = new int[maxSize];
top = -1; //设置空栈
}
//压栈
public boolean push(int data){
扫描二维码关注公众号,回复:
5520989 查看本文章
if(top == maxSize-1){
throw new RuntimeException("栈已满”);
}else{
this.data[top++] = data;
return true;
}
//弹出
public int pop throw Exception(){
if(top == -1){
throw new RuntimeException(“栈已空”)
}
}
}
}
//测试
class Test(){
public static void main(){
Stack stack = new Stack(6);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
while(stack.top >=0){
System.out.print(stack.pop())
}
}
}