Skip to content

队列

查看js的队列结构
js
class Queue {
    constructor(...args) {
        this.queue = [...args]
    }
    // 入队列
    enqueue(...items) {
        this.queue.push(...items)
    }
    // 出队列
    dequeue() {
        return this.queue.shift()
    }
    // 队头
    front() {
        return this.isEmpty() ? undefined : this.queue[0]
    }
    // 队尾
    back() {
        return this.isEmpty() ? undefined : this.queue[this.size() - 1]
    }
    // 是否为空
    isEmpty() {
        return this.size() === 0
    }
    // 队列长度
    size() {
        return this.queue.length
    }
}

延伸题目

  • 933.最新的请求次数(未完成)
  • 232.用栈实现队列:题解
  • 剑指 Offer 09. 用两个栈实现队列:题解
  • 225.用队列实现栈:题解

MIT Licensed