使用数组模拟实现栈

使用长度为5的数组模拟实现栈 先进后出的数据结构

package com.bjpowernode.test.datastructure;


 class MyStack {

    private Object [] datas;

    private int index;//只想下一个元素的引用或者下标

    public MyStack() {
        this.index = 0;
        datas= new Object[5];
    }


    /**
     * 压栈
     * @param obj
     */
    void push(Object obj) {
        int length = datas.length;
        if (index > length - 1) {
            //需要扩容或者抛异常
            throw new RuntimeException("栈已满");
        }
        datas[index] = obj;
        index++;
    }

    /**
     * 弹栈
     */
    Object pop() {
        if (index <= 0) {
            throw new RuntimeException("栈已空");
        }
        Object o = datas[index - 1];
        index--;
        return o;
    }
}
class TestStack {
    public static void main(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(1);
        myStack.push(1);
        myStack.push(1);
        myStack.push(1);
        myStack.push(1);
     //   myStack.push(1);
        System.out.println("=====");
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
        System.out.println(myStack.pop());
       // System.out.println(myStack.pop());

    }
}

 

posted @ 2019-11-07 16:53  土豆Z120553  阅读(227)  评论(0编辑  收藏  举报