java学习之—栈

/**
 * 栈
 * Create by Administrator
 * 2018/6/11 0011
 * 上午 10:20
 **/
public class StackX {

    private int maxSixe;

    private long[] stackArray;

    private int top;

    public StackX(int s) {
        this.maxSixe = s;
        this.stackArray = new long[maxSixe];
        this.top = -1;
    }

    /**
     * 入栈
     * @param j
     */
    public void push(long j){
        if(isFull()){
            System.out.println("栈已满");
        }else{
            this.stackArray[++top] = j;
        }
    }

    /**
     * 出栈
     */
    public long pop(){
        return stackArray[top--];
    }

    /**
     * 查看
     * @return
     */
    public void peek(){
        System.out.println(stackArray[top]);
    }

    /**
     * 判断栈是否为空
     * @return
     */
    public boolean isEmpty(){
        return (top == -1);
    }

    /**
     * 判断栈是否满了
     * @return
     */
    public boolean isFull(){
        return (top == maxSixe-1);
    }

    public static void main(String[] args) {
        StackX stackX = new StackX(5);
        stackX.push(20);
        stackX.push(60);
        stackX.push(40);
        stackX.push(80);
        stackX.push(90);
        stackX.push(95);

//        while (!stackX.isEmpty()){
//            long value = stackX.pop();//出栈
//            System.out.print("出栈:"+value);
//            System.out.print(" ");
//        }
        System.out.println("");

        stackX.peek();
    }

}
posted @ 2018-06-11 14:11  花儿为何那样红  阅读(217)  评论(0编辑  收藏  举报