public class Stack {
int[]a;
int maxSize;
int top;
public static void main(String[] args) {
Stack stack = new Stack(3);
stack.add(1);
stack.add(2);
stack.add(3);
stack.showAll();
stack.add(4);
}
public void showAll() {
if (isEmpty()) {
System.out.println("栈为空");
}
for (int i = 0; i <= top; i++) {
System.out.print(a[i]+" ");
}
}
public void add(int data) {
if (isFull()) {
throw new RuntimeException("栈溢出");
}
a[++top] = data;
}
public int del() {
if (isEmpty()) {
throw new RuntimeException("栈为空");
}
return a[top--];
}
public boolean isEmpty() {
return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}
public Stack(int maxSize) {
this.maxSize = maxSize;
a = new int[maxSize];
this.top = -1;
}
}