Skip to content
hewei edited this page Feb 19, 2022 · 1 revision
const items = Symbol('stackItems');

/**栈 */
class Stack{
    constructor(){
        this[items] = {};
        this.count = 0;
    }

    push(element){
        this[items][this.count] = element;
        this.count++;
    }

    pop(){
        if(this.isEmpty){
            return undefined;
        }
        const r = this[items][this.couunt];
        delete this[items][this.count];
        this.count--;
        return r;
    }

    size(){
        this.count;
    }

    isEmpty(){
        return this.count === 0;
    }
}

const s = new Stack();

s.push(1)

  • 数据结构与算法

Clone this wiki locally