顺序表实现栈

class MyStack <T>{
    public T[] elem;
    public int top;

    public MyStack(){
        this.elem = (T[])new Object[10];
    }

    private boolean isFull(){
        if(this.top == elem.length){
            return true;
        }
        return false;
    }

    public void push(T data){
        if(isFull()){
            elem = Arrays.copyOf(elem, elem.length*2);
        }
        elem[top++] = data;
        //this.top++;
    }

    private boolean isEmpty(){
        if(this.top == 0){

            throw new UnsupportedOperationException("栈空!");
        }
        return false;
    }

    public T pop(){
        isEmpty();
        return this.elem[--this.top];
    }

    public T peek(){
        isEmpty();
        return elem[this.top-1];
    }

    public int size(){
        return this.top;
    }

}
发布了28 篇原创文章 · 获赞 3 · 访问量 737

猜你喜欢

转载自blog.csdn.net/XDtobaby/article/details/103056590
今日推荐