队列
狐七 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
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