public class MyStack {
    private long[] arr;
    private int top;

    public MyStack(){
        arr = new long[10];
        top = -1;
    }

    public MyStack(int max){
        arr = new long[max];
        top = -1;
    }

    public void push(int value){
        arr[++top]=value;
//        top++;
//        arr[top]=value;
    }

    public long pop(){
//        top--;
//        return arr[top+1];
        return arr[top--];
    }

    public long peek(){
        return arr[top];
    }

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


}

测试

public class test {
    public static void main(String[] args) {
        MyStack myStack = new MyStack(4);
        myStack.push(23);
        myStack.push(24);
        myStack.push(28);
        myStack.push(2);
        System.out.println(myStack.isEmpty());

        while (!myStack.isEmpty()){
            System.out.println(myStack.pop()+" ");
        }
        System.out.println(myStack.isEmpty());
    }
}
posted @ 2020-03-11 17:39  姚狗蛋  阅读(75)  评论(0编辑  收藏  举报