/**
* @author YuanZhenhui
* @serialData 2006-9-10
* @version 1.0
*
*/
棧介面
public interface StackInterface {
public boolean isEmpty();
public boolean isFull();
public void push(Object oo) throws OverFlowException;
public Object pop()throws UnderFlowException;
public Object get();
}
//自訂異常
class OverFlowException extends Exception{
public OverFlowException(){
System.out.println("this is an exception");
}
}
class UnderFlowException extends Exception{
public UnderFlowException(){
}
}
public class MyStack implements StackInterface{
private Object table[];
// top為棧頂元素
private int top=-1;
// 構造n個儲存單元的棧
public MyStack(int n){
table=new Object[n];
}
public boolean isEmpty(){
return top==-1;
}
public boolean isFull(){
return top>=table.length-1;
}
public void push(Object oo) throws OverFlowException{
if(!this.isFull()){
top++;
table[top]=oo;
}else{
throw new OverFlowException();
}
}
public Object pop()throws UnderFlowException{
Object temp=null;
if(!this.isEmpty()){
temp=table[top];
table[top]=null;
top--;
}else{
throw new UnderFlowException();
}
return temp;
}
public Object get(){
if(!this.isEmpty()){
return table[top];
}else
return null;
}
}