java实现顺序栈


public class MyStack{
    Object[] data;
    int top;
    int maxSize;

    public MyStack(int maxSize)
    {   
        this.maxSize = maxSize;
        this.top = -1;
        this.data = new Object[this.maxSize];
    }

    public boolean isEmpty()
    {
        return this.top == -1;
    }

    public boolean isFull()
    {
        return this.top == maxSize - 1;
    }

    public boolean push(Object e)
    {
        if(isFull()) return false;
        this.data[++this.top]=e;
        return true;
    }

    public Object pop()
    {
        if(isEmpty()) return null;
        return this.data[top--];
    }

    public Object getTop()
    {
        if(isEmpty()) return null;
        return this.data[top];
    }

    public static void main(String[] args)
    {
            MyStack Stack = new MyStack(100);
            for(int i=0;i<10;i++)
            {
                Stack.push(i+1);
            }
            while(Stack.isEmpty()==false)
            {
                System.out.print(Stack.pop()+"\t");
            }
    }
}
posted @ 2016-07-21 18:26  岳麓丹枫  阅读(150)  评论(0编辑  收藏  举报