使用java实现简单的栈
栈:是先进后出
public class MyStack {
private int size;
private Object[] objects;
private int top;
public MyStack(int size) {
this.size = size;
this.objects = new Object[size];
this.top = 0;
}
public boolean isEmpty() {
return top == 0;
}
public boolean push(Object o) {
if (beyond(top+1)) {
top++;
this.objects[top] = o;
return true;
}
return false;
}
public Object pop() {
if (top >= 0) {
Object object = objects[top];
objects[top] = null;
top--;
return object;
}
return null;
}
private boolean beyond(int capacity) {
if (capacity >= this.size){
return false;
}
return true;
}
}