public class Stack {
public int[] elem;
public int usedSize;
public Stack() {
this.elem = new int[10];
}
//Check whether the Stack is full
public boolean isFull() {
return this.usedSize == this.elem.length;
}
//Check whether the Stack is empty
public boolean empty() {
return this.usedSize == 0;
}
//Push a value at the top of the Stack
public void push(int val) {
if (isFull()) {
this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
}
this.elem[usedSize++] = val;
}
//Pop the top value of the Stack
public int pop() {
if (this.empty()) {
System.out.println("栈为空!");
return -1;
} else {
return this.elem[--this.usedSize];
}
}
//Get the value at the top of the Stack
public int peek() {
if (this.empty()) {
System.out.println("栈为空!");
return -1;
} else {
return this.elem[usedSize - 1];
}
}
}
|