JavaScript——一个简单的队列Demo

前言

一个简单的队列示例

内容

class Queue {
    constructor() {
        this.items = {}
        this.headIndex = 0
        this.tailIndex = 0
    }

    enqueue(item) {
        this.items[this.tailIndex] = item
        this.tailIndex++
    }

    dequeue() {
        if (!this.isEmpty) return
        const item = this.items[this.headIndex]
        delete this.items[this.headIndex]
        this.headIndex++
        return item
    }

    get length() {
        return this.tailIndex - this.headIndex
    }

    get isEmpty() {
        return this.tailIndex
    }

}

posted @ 2022-03-17 16:55  。思索  阅读(48)  评论(0编辑  收藏  举报