用链表实现栈

class MyStack {
    private final static MyStack head = new MyStack();
    int val;
    MyStack next;

    public MyStack() {

    }

    private MyStack(int val) {
        this.val = val;
    }

    //入栈
    public boolean push(int val) {
        if (next == null) {
            next = new MyStack(val);
            return true;
        }
        MyStack dummy = next;
        while (dummy.next != null) {
            dummy = dummy.next;
        }
        dummy.next = new MyStack(val);
        return true;
    }
    
    //出栈
    public boolean outStack() {
        MyStack myStack = next;
        if (myStack == null) {
            System.out.println("栈空");
            return false;
        }
        while (myStack.next != null && myStack.next.next != null) {
            myStack = myStack.next;
        }
        myStack.next = null;
        return true;
    }

    //打印
    public void println() {
        MyStack dummy = next;
        if (dummy == null) {
            System.out.println("栈空");
        }
        while (dummy != null) {
            System.out.println(dummy.val);
            dummy = dummy.next;
        }
    }

}

上一篇:MySQL异步驱动aiomysql


下一篇:android frecso+retrofit+tablayout