Deque(double-ended queue) [双端队列]
# 介绍
双端队列(deque,全名 double-ended queue)是一种具有队列和栈性质的抽象数据类型。双端队列中的元素可以从两端弹出,插入和删除操作限定在队列的两边进行。
# 操作
双端队列可以在队列任意一端入队和出队。此外,经常还会有一个查看(Peek)操作,返回该端的数据而不将其出队。
操作的名称依语言的不同而不同;主流实现包括:
注意:JavaScript 的数组就是一个双端队列。
# 实现
# JavaScript
class Deque {
/**
* Creates a deque
* @param {array} [elements]
*/
constructor(elements) {
this._backElements = Array.isArray(elements) ? elements : [];
this._frontElements = [];
this._backOffset = 0;
this._frontOffset = 0;
}
/**
* Adds an element at the front of the queue
* @public
* @param {any} element
*/
pushFront(element) {
this._frontElements.push(element);
return this;
}
/**
* Adds an element at the back of the queue
* @public
* @param {any} element
*/
pushBack(element) {
this._backElements.push(element);
return this;
}
/**
* Dequeues the front element in the queue
* @public
* @returns {any}
*/
popFront() {
if (this.size() === 0) {
return null;
}
if (this._frontElements.length > 0) {
const front = this._frontElements.pop();
if (this._frontOffset >= this._frontElements.length) {
this._frontElements = this._frontElements.slice(this._frontOffset);
this._frontOffset = 0;
}
return front;
}
const front = this.front();
this._backOffset += 1;
if (this._backOffset * 2 < this._backElements.length) {
return front;
}
this._backElements = this._backElements.slice(this._backOffset);
this._backOffset = 0;
return front;
}
/**
* Dequeues the back element of the queue
* @public
* @returns {any}
*/
popBack() {
if (this.size() === 0) {
return null;
}
if (this._backElements.length > 0) {
const back = this._backElements.pop();
if (this._backOffset >= this._backElements.length) {
this._backElements = this._backElements.slice(this._backOffset);
this._backOffset = 0;
}
return back;
}
const back = this.back();
this._frontOffset += 1;
if (this._frontOffset * 2 < this._frontElements.length) {
return back;
}
this._frontElements = this._frontElements.slice(this._frontOffset);
this._frontOffset = 0;
return back;
}
/**
* Returns the front element of the queue
* @public
* @returns {any}
*/
front() {
if (this.size() === 0) {
return null;
}
if (this._frontElements.length > 0) {
return this._frontElements[this._frontElements.length - 1];
}
return this._backElements[this._backOffset];
}
/**
* Returns the back element of the queue
* @public
* @returns {any}
*/
back() {
if (this.size() === 0) {
return null;
}
if (this._backElements.length > 0) {
return this._backElements[this._backElements.length - 1];
}
return this._frontElements[this._frontOffset];
}
/**
* Returns the number of elements in the deque
* @public
* @returns {number}
*/
size() {
const frontSize = this._frontElements.length - this._frontOffset;
const backSize = this._backElements.length - this._backOffset;
return frontSize + backSize;
}
/**
* Checks if the queue is empty
* @public
* @returns {boolean}
*/
isEmpty() {
return this.size() === 0;
}
/**
* Returns the remaining elements in the queue as an array
* @public
* @returns {array}
*/
toArray() {
const backElements = this._backElements.slice(this._backOffset);
const frontElements = this._frontElements.slice(this._frontElements);
return frontElements.reverse().concat(backElements);
}
/**
* Clears the queue
* @public
*/
clear() {
this._backElements = [];
this._frontElements = [];
this._backOffset = 0;
this._frontOffset = 0;
}
/**
* Creates a shallow copy of the queue
* @public
* @return {Deque}
*/
clone() {
return new Deque(this.toArray());
}
/**
* Creates a deque from an existing array
* @public
* @static
* @param {array} elements
* @return {Deque}
*/
static fromArray(elements) {
return new Deque(elements);
}
}
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# 参考
编辑 (opens new window)
上次更新: 2022/10/11, 17:42:20