队列

algorithm

# 队列

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
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

# 延伸题目

  • 933.最新的请求次数(未完成)
  • 232.用栈实现队列:题解
  • 剑指 Offer 09. 用两个栈实现队列:题解
  • 225.用队列实现栈:题解
更新时间: 2022-03-25 17:04