学习记录=>数组模拟实现的栈
class ArrayStack {
private int maxSize;
private int[] stack;
private int top = -1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new int[maxSize];
}
public boolean isFull() {
return top == maxSize - 1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int value) {
if (isFull()) {
System.out.println("栈已满");
return;
}
top++;
stack[top] = value;
}
public int pop() {
if (isEmpty()) {
throw new RuntimeException("栈空");
}
int value = stack[top];
stack[top] = 0;
top--;
return value;
}
public void list() {
for (int i = top; i > -1; i--) {
System.out.println("当前遍历到的栈元素:" + stack[i]);
}
}
}