# 基本概念
- 双端队列是一种允许我们同时从前端和后端添加和移除元素的特殊队列。
# 代码实现
// 双端队列
class Deque {
constructor() {
this.deque = [];
}
// 在双端队列的前面添加新元素
addFront (el) {
this.deque.unshift(el);
return this.deque;
}
// 在双端队列的尾部添加元素
addBack(el) {
this.deque.push(el);
return this.deque;
}
// 移除队列前端的元素
removeFront () {
return this.deque.shift();
}
// 移除队列末尾元素
removeBack () {
return this.deque.pop();
}
// 返回队列首位元素
peekFront() {
return this.deque[0];
}
// 返回队列末尾元素
peekBack() {
return this.deque[this.deque.length - 1];
}
}
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
30
31
32
33
34
35
36
37
38
39
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
30
31
32
33
34
35
36
37
38
39