package stack;
import java.util.EmptyStackException;
public class MyStack<E> {
//Stack 的数组实现
private int CAPACITY;
private Object[] theArray;
private int topOfStack;
public MyStack() {
CAPACITY = 100;
topOfStack = -1;
theArray = new Object[CAPACITY];
}
public MyStack(int cAPACITY) {
CAPACITY = cAPACITY;
topOfStack = -1;
theArray = new Object[CAPACITY];
}
public boolean empty() {
return topOfStack == -1;
}
public Object peak() {
if (this.empty())
throw new EmptyStackException();
return theArray[topOfStack];
}
public int size() {
return topOfStack+1;
}
public void push(E value) {
theArray[++topOfStack] = value;
}
public Object pop() {
if (topOfStack != -1) {
return theArray[topOfStack--];
}
return null;
}
}
数组实现stack
最新推荐文章于 2024-04-20 23:21:06 发布