注册 登录
编程论坛 J2EE论坛

求高手用栈实现一个迷宫的问题

格斗小子 发布于 2010-11-03 21:35, 556 次点击
这是我的栈:
public class SeqStack<E> implements SStack<E> {
    private Object value[];//存储栈的数据元素的数组
    private int top;// 栈顶元素
    public SeqStack(int capacity){//构造指定长度的顺序栈
        this.value=new Object[Math.abs(capacity)];
        this.top=-1;
    }
    public SeqStack(){//够着默认容量的栈
        this(10);
    }
    public boolean isEmpty() {//判断栈空
        return this.top==-1;
        
    }
    public boolean push(E element) {//如栈
        if(element==null){
            return false;
        }
        if(this.top==value.length-1){
            Object temp[]=this.value;
            this.value=new Object[temp.length*2];
            for(int i=0;i<temp.length;i++){
                this.value[i]=temp[i];
            }
        }
        this.top++;
        this.value[this.top]=element;
        return true;
    }
    public E pop() {
        if(!isEmpty()){
            return (E)this.value[this.top--];
        }
        else
            return null;
    }
    public E get() {
        if(!isEmpty()){
            return (E)this.value[this.top];
        }
        else
            return null;
    }
    public String  toString(){
        String str= "(";
        for(int i=0;i<this.value.length;i++){
        str+=this.value[i].toString();
        }
        return str+")";
        
    }
}


package cha3;

public interface SStack<E> {
    boolean isEmpty();
    boolean push(E element);//入栈
    E pop();//出栈
    E get();//取栈顶元素
}
0 回复
1