Stack

72 阅读1分钟

// 栈
class Stack {
    constructor() {
        this.store = [];
    }
    // 栈顶元素
    top() {
        const item = this.out();
        this.in(item);
        return item;
    }
    // 反转栈
    // save = true  反转
    // save = false 不反转
    reverse(save = false) {
        let cache = [...this.store].reverse();

        if (save) {
            this.store = cache;
        }

        return cache;
    }
    // 入栈
    in(data) {
        this.store.push(data);
    }
    // 出栈(栈尾),并返回该元素。
    out() {
        return this.store.pop();
    }
    // 出栈(栈顶),并返回该元素。
    reOut() {
        return this.store.shift();
    }
    // 空  : true
    // 非空: false
    isNull() {
        return !this.store.length;
    }
    // 栈高
    len() {
        return this.store.length;
    }
}

export default { Stack };