代码如下:
public static void main(String[] args) {
Stack stack = new Stack(5);
stack.push(1);
stack.push(4);
stack.push(3);
stack.push(2);
stack.push(5);
while(stack.top>=0) {
int b = stack.pop();
System.out.println(b);
}
}
}
class Stack{
int[] data;
int maxSize;
int top;
public Stack(int maxSize) {
this.maxSize = maxSize;
this.data=new int[maxSize];
top = -1;
}
public boolean push(int a) {
if(top>=maxSize) {
System.out.println("栈已经满了");
return false;
} else {
data[++top] = a;
return true;
}
}
public int pop() {
if(top<0) {
System.out.println("栈为空");
return 0;
} else {
int b = data[top--];
return b;
}
}