• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Linear Container Deque
2
3> **NOTE**
4>
5> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version.
6
7Double-ended queue (deque) is a sequence container implemented based on the queue data structure that follows the principles of First In First Out (FIFO) and Last In First Out (LIFO). It allows insertion and removal of elements at both the ends. **Deque** can dynamically adjust the capacity based on project requirements. It doubles the capacity each time. **Deque** differs from **[Queue](js-apis-queue.md)** and **[Vector](js-apis-vector.md)** mainly in the following aspects:
8
9**Queue** follows the principle of FIFO only and allows element removal at the front and insertion at the rear.
10
11**Vector** supports insertion and deletion of elements in between, as well asat both the ends. When compared with **Vector**, **Deque** is more efficient in inserting and removing header elements, but less efficient in accessing elements.
12
13**Recommended use case**: Use **Deque** when you need to frequently insert or remove elements at both the ends of a container.
14
15## Modules to Import
16
17```ts
18import Deque from '@ohos.util.Deque';
19```
20
21## Deque
22
23### Attributes
24
25**System capability**: SystemCapability.Utils.Lang
26
27| Name| Type| Readable| Writable| Description|
28| -------- | -------- | -------- | -------- | -------- |
29| length | number | Yes| No| Number of elements in a deque (called container later).|
30
31### constructor
32
33constructor()
34
35A constructor used to create a **Deque** instance.
36
37**System capability**: SystemCapability.Utils.Lang
38
39**Example**
40
41```ts
42let deque = new Deque();
43```
44
45### insertFront
46
47insertFront(element: T): void
48
49Inserts an element at the front of this container.
50
51**System capability**: SystemCapability.Utils.Lang
52
53**Parameters**
54
55| Name| Type| Mandatory| Description|
56| -------- | -------- | -------- | -------- |
57| element | T | Yes| Target element.|
58
59**Example**
60
61```ts
62let deque = new Deque();
63deque.insertFront("a");
64deque.insertFront(1);
65let b = [1, 2, 3];
66deque.insertFront(b);
67let c = {name : "lala", age : "13"};
68deque.insertFront(false);
69```
70
71### insertEnd
72
73insertEnd(element: T): void
74
75Inserts an element at the end of this container.
76
77**System capability**: SystemCapability.Utils.Lang
78
79**Parameters**
80
81| Name| Type| Mandatory| Description|
82| -------- | -------- | -------- | -------- |
83| element | T | Yes| Target element.|
84
85**Example**
86
87```ts
88let deque = new Deque();
89deque.insertEnd("a");
90deque.insertEnd(1);
91let b = [1, 2, 3];
92deque.insertEnd(b);
93let c = {name : "lala", age : "13"};
94deque.insertEnd(false);
95```
96
97### has
98
99has(element: T): boolean
100
101Checks whether this container has the specified element.
102
103**System capability**: SystemCapability.Utils.Lang
104
105**Parameters**
106
107| Name| Type| Mandatory| Description|
108| -------- | -------- | -------- | -------- |
109| element | T | Yes| Target element.|
110
111**Return value**
112
113| Type| Description|
114| -------- | -------- |
115| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
116
117**Example**
118
119```ts
120let deque = new Deque();
121let result = deque.has("Ahfbrgrbgnutfodgorrogorg");
122deque.insertFront("Ahfbrgrbgnutfodgorrogorg");
123let result1 = deque.has("Ahfbrgrbgnutfodgorrogorg");
124```
125
126### popFirst
127
128popFirst(): T
129
130Removes the first element of this container.
131
132**System capability**: SystemCapability.Utils.Lang
133
134**Return value**
135
136| Type| Description|
137| -------- | -------- |
138| T | Element removed.|
139
140**Example**
141
142```ts
143let deque = new Deque();
144deque.insertFront(2);
145deque.insertFront(4);
146deque.insertEnd(5);
147deque.insertFront(2);
148deque.insertFront(4);
149let result = deque.popFirst();
150```
151
152### popLast
153
154popLast(): T
155
156Removes the last element of this container.
157
158**System capability**: SystemCapability.Utils.Lang
159
160**Return value**
161
162| Type| Description|
163| -------- | -------- |
164| T | Element removed.|
165
166**Example**
167
168```ts
169let deque = new Deque();
170deque.insertFront(2);
171deque.insertEnd(4);
172deque.insertFront(5);
173deque.insertFront(2);
174deque.insertFront(4);
175let result = deque.popLast();
176```
177
178### forEach
179
180forEach(callbackfn: (value: T, index?: number, deque?: Deque<T>) => void,
181thisArg?: Object): void
182
183Uses a callback to traverse the elements in this container and obtain their position indexes.
184
185**System capability**: SystemCapability.Utils.Lang
186
187**Parameters**
188
189| Name| Type| Mandatory| Description|
190| -------- | -------- | -------- | -------- |
191| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
192| thisArg | Object | No| Value to use when the callback is invoked.|
193
194callbackfn
195
196| Name| Type| Mandatory| Description|
197| -------- | -------- | -------- | -------- |
198| value | T | Yes| Value of the element that is currently traversed.|
199| index | number | No| Position index of the element that is currently traversed.|
200| deque | Deque<T> | No| Instance that invokes the **forEach** method.|
201
202**Example**
203
204```ts
205let deque = new Deque();
206deque.insertFront(2);
207deque.insertEnd(4);
208deque.insertFront(5);
209deque.insertEnd(4);
210deque.forEach((value, index) => {
211    console.log("value:" + value, "index:" + index);
212});
213```
214
215### getFirst
216
217getFirst(): T
218
219Obtains the first element of this container.
220
221**System capability**: SystemCapability.Utils.Lang
222
223**Return value**
224
225| Type| Description|
226| -------- | -------- |
227| T | Element obtained.|
228
229**Example**
230
231```ts
232let deque = new Deque();
233deque.insertEnd(2);
234deque.insertEnd(4);
235deque.insertFront(5);
236deque.insertFront(4);
237let result = deque.getFirst();
238```
239
240### getLast
241
242getLast(): T
243
244Obtains the last element of this container.
245
246**System capability**: SystemCapability.Utils.Lang
247
248**Return value**
249
250| Type| Description|
251| -------- | -------- |
252| T | Element obtained.|
253
254**Example**
255
256```ts
257let deque = new Deque();
258deque.insertFront(2);
259deque.insertFront(4);
260deque.insertFront(5);
261deque.insertFront(4);
262let result = deque.getLast();
263```
264
265### [Symbol.iterator]
266
267[Symbol.iterator]\(): IterableIterator<T>
268
269Obtains an iterator, each item of which is a JavaScript object.
270
271**System capability**: SystemCapability.Utils.Lang
272
273**Return value**
274
275| Type| Description|
276| -------- | -------- |
277| IterableIterator<T> | Iterator obtained.|
278
279**Example**
280```ts
281let deque = new Deque();
282deque.insertFront(2);
283deque.insertFront(4);
284deque.insertFront(5);
285deque.insertFront(4);
286
287// Method 1:
288for (let item of deque) {
289  console.log("value:" + item);
290}
291
292// Method 2:
293let iter = deque[Symbol.iterator]();
294let temp = iter.next().value;
295while(temp != undefined) {
296  console.log("value:" + temp);
297  temp = iter.next().value;
298}
299```
300