public class SeqStack{
final int defaultSize = 10;
int top;
Object[] stack;
int maxStackSize;
public SeqStack(){
initiate(defaultSize);
}
public SeqStack(int sz){
initiate(sz);
}
private void initiate(int sz){
maxStackSize = sz;
top = 0;
stack = new Object[sz];
}
public void push(Object obj) throws Exception{ //把数据元素obj插入堆栈
if(top == maxStackSize){
throw new Exception("堆栈已满!");
}
stack[top] = obj;
top ++;
}
public Object pop() throws Exception{ //出栈,删除的数据元素由函数返回
if(top == 0){
throw new Exception("堆栈已空!");
}
top --;
return stack[top];
}
public Object getTop() throws Exception{ //取堆栈当前栈顶的数据元素并由函数返回
if(top == 0){
throw new Exception("堆栈已空!");
}
return stack[top - 1];
}
public boolean notEmpty(){ //若当前非空则函数返回true,否则函数返回false
return (top > 0);
}
}