package com.fxr.stack;

/**
 * Created by airycode on 2017/3/12.
 */
public class MyStack {
    private long [] arr;
    private int top;

    /**
     *默认的构造方法
     */
    public MyStack(){
        arr = new long[10];
        top = -1;
    }
    /**
     * 带参数的构造方法
     */
    public MyStack(int maxSize){
        arr = new long[maxSize];
        top = -1;
    }

    //添加数据
    public void push(long value){
        arr[++top] = value;
    }
    //删除数据
    public long pop(){
        return arr[top--];
    }
    //查看数据
    public long peek(){
        return arr[top];
    }
    //判断是不是为空
    public boolean isEmpty(){
        return top == -1;
    }
    //判断是不是满
    public boolean isFull(){
        return top == arr.length-1;
    }

}


------------------------------------------------------------------------------------------------
package com.fxr.stack;

/**
 * Created by airycode on 2017/3/12.
 */
public class TestMyStack {

    public static void main(String[] args){
        MyStack myStack = new MyStack(4);
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);
        myStack.push(4);

        System.out.println(myStack.isEmpty());
        System.out.println(myStack.isFull());
        System.out.println(myStack.peek());

    }

}

  

原文地址:https://www.cnblogs.com/airycode/p/6537981.html