• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Linear Container Stack
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
7**Stack** is implemented based on the array data structure. It follows the principle Last Out First In (LOFI) and supports data insertion and removal at one end.
8
9Unlike **[Queue](js-apis-queue.md)**, which is implemented based on the queue data structure and supports insertion at one end and removal at the other end, **Stack** supports insertion and removal at the same end.
10
11**Recommended use case**: Use **Stack** in LOFI scenarios.
12
13## Modules to Import
14
15```ts
16import Stack from '@ohos.util.Stack';
17```
18
19
20
21
22## Stack
23
24### Attributes
25
26**System capability**: SystemCapability.Utils.Lang
27
28| Name| Type| Readable| Writable| Description|
29| -------- | -------- | -------- | -------- | -------- |
30| length | number | Yes| No| Number of elements in a stack (called container later).|
31
32
33### constructor
34
35constructor()
36
37A constructor used to create a **Stack** instance.
38
39**System capability**: SystemCapability.Utils.Lang
40
41**Example**
42
43```ts
44let stack = new Stack();
45```
46
47
48### push
49
50push(item: T): T
51
52Adds an element at the top of this container.
53
54**System capability**: SystemCapability.Utils.Lang
55
56**Parameters**
57
58| Name| Type| Mandatory| Description|
59| -------- | -------- | -------- | -------- |
60| item | T | Yes| Target element.|
61
62**Return value**
63
64| Type| Description|
65| -------- | -------- |
66| T | Element added.|
67
68**Example**
69
70```ts
71let stack = new Stack();
72let result = stack.push("a");
73let result1 = stack.push(1);
74let b = [1, 2, 3];
75stack.push(b);
76let c = {name : "lala", age : "13"};
77let result3 = stack.push(c);
78```
79
80### pop
81
82pop(): T
83
84Removes the top element from this container.
85
86**System capability**: SystemCapability.Utils.Lang
87
88**Return value**
89
90| Type| Description|
91| -------- | -------- |
92| T | Element removed.|
93
94**Example**
95
96```ts
97let stack = new Stack();
98stack.push(2);
99stack.push(4);
100stack.push(5);
101stack.push(2);
102stack.push(4);
103let result = stack.pop();
104```
105
106### peek
107
108peek(): T
109
110Obtains the top element of this container.
111
112**System capability**: SystemCapability.Utils.Lang
113
114**Return value**
115
116| Type| Description|
117| -------- | -------- |
118| T | Element obtained.|
119
120**Example**
121
122```ts
123let stack = new Stack();
124stack.push(2);
125stack.push(4);
126stack.push(5);
127stack.push(2);
128let result = stack.peek();
129```
130
131### locate
132
133locate(element: T): number
134
135Obtains the index of the first occurrence of the specified element in this container.
136
137**System capability**: SystemCapability.Utils.Lang
138
139**Parameters**
140
141| Name| Type| Mandatory| Description|
142| -------- | -------- | -------- | -------- |
143| element | T | Yes| Target element.|
144
145**Return value**
146
147| Type| Description|
148| -------- | -------- |
149| number | Returns the position index if obtained; returns **-1** otherwise.|
150
151**Example**
152
153```ts
154let stack = new Stack();
155stack.push(2);
156stack.push(4);
157stack.push(5);
158stack.push(2);
159let result = stack.locate(2);
160```
161
162### forEach
163
164forEach(callbackfn: (value: T, index?: number, stack?: Stack<T>) => void,
165thisArg?: Object): void
166
167Uses a callback to traverse the elements in this container and obtain their position indexes.
168
169**System capability**: SystemCapability.Utils.Lang
170
171**Parameters**
172
173| Name| Type| Mandatory| Description|
174| -------- | -------- | -------- | -------- |
175| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
176| thisArg | Object | No| Value to use when the callback is invoked.|
177
178callbackfn
179
180| Name| Type| Mandatory| Description|
181| -------- | -------- | -------- | -------- |
182| value | T | Yes| Value of the element that is currently traversed.|
183| index | number | No| Position index of the element that is currently traversed.|
184| stack | Stack<T> | No| Instance that invokes the **forEach** method.|
185
186**Example**
187
188```ts
189let stack = new Stack();
190stack.push(2);
191stack.push(4);
192stack.push(5);
193stack.push(4);
194stack.forEach((value, index) => {
195    console.log("value:" + value, "index:" + index);
196});
197```
198
199### isEmpty
200
201isEmpty(): boolean
202
203Checks whether this container is empty (contains no elements).
204
205**System capability**: SystemCapability.Utils.Lang
206
207**Return value**
208
209| Type| Description|
210| -------- | -------- |
211| boolean | Returns **true** if the container is empty; returns **false** otherwise.|
212
213**Example**
214
215```ts
216let stack = new Stack();
217stack.push(2);
218stack.push(4);
219stack.push(5);
220stack.push(4);
221let result = stack.isEmpty();
222```
223
224### [Symbol.iterator]
225
226[Symbol.iterator]\(): IterableIterator<T>
227
228Obtains an iterator, each item of which is a JavaScript object.
229
230**System capability**: SystemCapability.Utils.Lang
231
232**Return value**
233
234| Type| Description|
235| -------- | -------- |
236| IterableIterator<T> | Iterator obtained.|
237
238**Example**
239```ts
240let stack = new Stack();
241stack.push(2);
242stack.push(4);
243stack.push(5);
244stack.push(4);
245
246// Method 1:
247for (let item of stack) {
248  console.log("value:" + item);
249}
250
251// Method 2:
252let iter = stack[Symbol.iterator]();
253let temp = iter.next().value;
254while(temp != undefined) {
255  console.log("value:" + temp);
256  temp = iter.next().value;
257}
258```
259