• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Linear Container LinkedList
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**LinkedList** is implemented based on the doubly linked list. Each node of the doubly linked list has references pointing to the previous element and the next element. When querying an element, the system traverses the list from the beginning or end. **LinkedList** offers efficient insertion and removal operations but supports low query efficiency. **LinkedList** allows null elements.
8
9Unlike **[List](js-apis-list.md)**, which is a singly linked list, **LinkedList** is a doubly linked list that supports insertion and removal at both ends.
10
11**LinkedList** is less efficient in data access than **[ArrayList](js-apis-arraylist.md)**.
12
13**Recommended use case**: Use **LinkedList** for frequent insertion and removal operations.
14
15## Modules to Import
16
17```ts
18import LinkedList from '@ohos.util.LinkedList';
19```
20
21
22
23
24## LinkedList
25
26### Attributes
27
28**System capability**: SystemCapability.Utils.Lang
29
30| Name| Type| Readable| Writable| Description|
31| -------- | -------- | -------- | -------- | -------- |
32| length | number | Yes| No| Number of elements in a linked list (called container later).|
33
34
35### constructor
36
37constructor()
38
39A constructor used to create a **LinkedList** instance.
40
41**System capability**: SystemCapability.Utils.Lang
42
43
44**Example**
45
46```ts
47let linkedList = new LinkedList();
48```
49
50
51### add
52
53add(element: T): boolean
54
55Adds an element at the end of this container.
56
57**System capability**: SystemCapability.Utils.Lang
58
59**Parameters**
60
61| Name| Type| Mandatory| Description|
62| -------- | -------- | -------- | -------- |
63| element | T | Yes| Target element.|
64
65**Return value**
66
67| Type| Description|
68| -------- | -------- |
69| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
70
71**Example**
72
73```ts
74let linkedList = new LinkedList();
75let result = linkedList.add("a");
76let result1 = linkedList.add(1);
77let b = [1, 2, 3];
78linkedList.add(b);
79let c = {name : "lala", age : "13"};
80let result3 = linkedList.add(false);
81```
82
83### addFirst
84
85addFirst(element: T): void
86
87Adds an element at the top of this container.
88
89**System capability**: SystemCapability.Utils.Lang
90
91**Parameters**
92
93| Name| Type| Mandatory| Description|
94| -------- | -------- | -------- | -------- |
95| element | T | Yes| Target element.|
96
97**Example**
98
99```ts
100let linkedList = new LinkedList();
101linkedList.addFirst("a");
102linkedList.addFirst(1);
103let b = [1, 2, 3];
104linkedList.addFirst(b);
105let c = {name : "lala", age : "13"};
106linkedList.addFirst(false);
107```
108
109### insert
110
111insert(index: number, element: T): void
112
113Inserts an element at the specified position in this container.
114
115**System capability**: SystemCapability.Utils.Lang
116
117**Parameters**
118
119| Name| Type| Mandatory| Description|
120| -------- | -------- | -------- | -------- |
121| element | T | Yes| Target element.|
122| index | number | Yes| Index of the position where the element is to be inserted.|
123
124**Example**
125
126```ts
127let linkedList = new LinkedList();
128linkedList.insert(0, "A");
129linkedList.insert(1, 0);
130linkedList.insert(2, true);
131```
132
133### has
134
135has(element: T): boolean
136
137Checks whether this container has the specified element.
138
139**System capability**: SystemCapability.Utils.Lang
140
141**Parameters**
142
143| Name| Type| Mandatory| Description|
144| -------- | -------- | -------- | -------- |
145| element | T | Yes| Target element.|
146
147**Return value**
148
149| Type| Description|
150| -------- | -------- |
151| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
152
153**Example**
154
155```ts
156let linkedList = new LinkedList();
157let result1 = linkedList.has("Ahfbrgrbgnutfodgorrogorg");
158linkedList.add("Ahfbrgrbgnutfodgorrogorg");
159let result = linkedList.has("Ahfbrgrbgnutfodgorrogorg");
160```
161
162### get
163
164get(index: number): T
165
166Obtains an element at the specified position in this container.
167
168**System capability**: SystemCapability.Utils.Lang
169
170**Parameters**
171
172| Name| Type| Mandatory| Description|
173| -------- | -------- | -------- | -------- |
174| index | number | Yes| Position index of the target element.|
175
176**Return value**
177
178| Type| Description|
179| -------- | -------- |
180| T | Element obtained.|
181
182**Example**
183
184```ts
185let linkedList = new LinkedList();
186linkedList.add(2);
187linkedList.add(4);
188linkedList.add(5);
189linkedList.add(2);
190linkedList.add(1);
191linkedList.add(2);
192linkedList.add(4);
193let result = linkedList.get(2);
194```
195
196### getLastIndexOf
197
198getLastIndexOf(element: T): number
199
200Obtains the index of the last occurrence of the specified element in this container.
201
202**System capability**: SystemCapability.Utils.Lang
203
204**Parameters**
205
206| Name| Type| Mandatory| Description|
207| -------- | -------- | -------- | -------- |
208| element | T | Yes| Target element.|
209
210**Return value**
211
212| Type| Description|
213| -------- | -------- |
214| number | Returns the position index if obtained; returns **-1** otherwise.|
215
216**Example**
217
218```ts
219let linkedList = new LinkedList();
220linkedList.add(2);
221linkedList.add(4);
222linkedList.add(5);
223linkedList.add(2);
224linkedList.add(1);
225linkedList.add(2);
226linkedList.add(4);
227let result = linkedList.getLastIndexOf(2);
228```
229
230### getIndexOf
231
232getIndexOf(element: T): number
233
234Obtains the index of the first occurrence of the specified element in this container.
235
236**System capability**: SystemCapability.Utils.Lang
237
238**Parameters**
239
240| Name| Type| Mandatory| Description|
241| -------- | -------- | -------- | -------- |
242| element | T | Yes| Target element.|
243
244**Return value**
245
246| Type| Description|
247| -------- | -------- |
248| number | Returns the position index if obtained; returns **-1** otherwise.|
249
250**Example**
251
252```ts
253let linkedList = new LinkedList();
254linkedList.add(2);
255linkedList.add(4);
256linkedList.add(5);
257linkedList.add(2);
258linkedList.add(1);
259linkedList.add(2);
260linkedList.add(4);
261let result = linkedList.getIndexOf(2);
262```
263
264### removeByIndex
265
266removeByIndex(index: number): T
267
268Removes an element at the specified position from this container.
269
270**System capability**: SystemCapability.Utils.Lang
271
272**Parameters**
273
274| Name| Type| Mandatory| Description|
275| -------- | -------- | -------- | -------- |
276| index | number | Yes| Position index of the target element.|
277
278**Return value**
279
280| Type| Description|
281| -------- | -------- |
282| T | Element removed.|
283
284**Example**
285
286```ts
287let linkedList = new LinkedList();
288linkedList.add(2);
289linkedList.add(4);
290linkedList.add(5);
291linkedList.add(2);
292linkedList.add(4);
293let result = linkedList.removeByIndex(2);
294```
295
296### removeFirst
297
298removeFirst(): T
299
300Removes the first element from this container.
301
302**System capability**: SystemCapability.Utils.Lang
303
304**Return value**
305
306| Type| Description|
307| -------- | -------- |
308| T | Element removed.|
309
310**Example**
311
312```ts
313let linkedList = new LinkedList();
314linkedList.add(2);
315linkedList.add(4);
316linkedList.add(5);
317linkedList.add(2);
318linkedList.add(4);
319let result = linkedList.removeFirst();
320```
321
322### removeLast
323
324removeLast(): T
325
326Removes the last element from this container.
327
328**System capability**: SystemCapability.Utils.Lang
329
330**Return value**
331
332| Type| Description|
333| -------- | -------- |
334| T | Element removed.|
335
336**Example**
337
338```ts
339let linkedList = new LinkedList();
340linkedList.add(2);
341linkedList.add(4);
342linkedList.add(5);
343linkedList.add(2);
344linkedList.add(4);
345let result = linkedList.removeLast();
346```
347
348### remove
349
350remove(element: T): boolean
351
352Removes the first occurrence of the specified element from this container.
353
354**System capability**: SystemCapability.Utils.Lang
355
356**Parameters**
357
358| Name| Type| Mandatory| Description|
359| -------- | -------- | -------- | -------- |
360| element | T | Yes| Target element.|
361
362**Return value**
363
364| Type| Description|
365| -------- | -------- |
366| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
367
368**Example**
369
370```ts
371let linkedList = new LinkedList();
372linkedList.add(2);
373linkedList.add(4);
374linkedList.add(5);
375linkedList.add(4);
376let result = linkedList.remove(2);
377```
378
379### removeFirstFound
380
381removeFirstFound(element: T): boolean
382
383Removes the first occurrence of the specified element from this container.
384
385**System capability**: SystemCapability.Utils.Lang
386
387**Parameters**
388
389| Name| Type| Mandatory| Description|
390| -------- | -------- | -------- | -------- |
391| element | T | Yes| Target element.|
392
393**Return value**
394
395| Type| Description|
396| -------- | -------- |
397| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
398
399**Example**
400
401```ts
402let linkedList = new LinkedList();
403linkedList.add(2);
404linkedList.add(4);
405linkedList.add(5);
406linkedList.add(4);
407let result = linkedList.removeFirstFound(4);
408```
409
410### removeLastFound
411
412removeLastFound(element: T): boolean
413
414Removes the last occurrence of the specified element from this container.
415
416**System capability**: SystemCapability.Utils.Lang
417
418**Parameters**
419
420| Name| Type| Mandatory| Description|
421| -------- | -------- | -------- | -------- |
422| element | T | Yes| Target element.|
423
424**Return value**
425
426| Type| Description|
427| -------- | -------- |
428| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
429
430**Example**
431
432```ts
433let linkedList = new LinkedList();
434linkedList.add(2);
435linkedList.add(4);
436linkedList.add(5);
437linkedList.add(4);
438let result = linkedList.removeLastFound(4);
439```
440
441### clone
442
443clone(): LinkedList<T>
444
445Clones this container and returns a copy. The modification to the copy does not affect the original instance.
446
447**System capability**: SystemCapability.Utils.Lang
448
449**Return value**
450
451| Type| Description|
452| -------- | -------- |
453| LinkedList<T> | New **LinkedList** instance obtained.|
454
455**Example**
456
457```ts
458let linkedList = new LinkedList();
459linkedList.add(2);
460linkedList.add(4);
461linkedList.add(5);
462linkedList.add(4);
463let result = linkedList.clone();
464```
465
466### forEach
467
468forEach(callbackfn: (value: T, index?: number, LinkedList?: LinkedList<T>) => void,
469thisArg?: Object): void
470
471Uses a callback to traverse the elements in this container and obtain their position indexes.
472
473**System capability**: SystemCapability.Utils.Lang
474
475**Parameters**
476
477| Name| Type| Mandatory| Description|
478| -------- | -------- | -------- | -------- |
479| callbackfn | function | Yes| Callback invoked to traverse the elements in the container.|
480| thisArg | Object | No| Value to use when the callback is invoked.|
481
482callbackfn
483
484| Name| Type| Mandatory| Description|
485| -------- | -------- | -------- | -------- |
486| value | T | Yes| Value of the element that is currently traversed.|
487| index | number | No| Position index of the element that is currently traversed.|
488| LinkedList | LinkedList<T> | No| Instance that invokes the **forEach** API.|
489
490**Example**
491
492```ts
493let linkedList = new LinkedList();
494linkedList.add(2);
495linkedList.add(4);
496linkedList.add(5);
497linkedList.add(4);
498linkedList.forEach((value, index) => {
499    console.log("value:" + value, "index:" + index);
500});
501```
502
503### clear
504
505clear(): void
506
507Clears this container and sets its length to **0**.
508
509**System capability**: SystemCapability.Utils.Lang
510
511**Example**
512
513```ts
514let linkedList = new LinkedList();
515linkedList.add(2);
516linkedList.add(4);
517linkedList.add(5);
518linkedList.add(4);
519linkedList.clear();
520```
521
522### set
523
524set(index: number, element: T): T
525
526Replaces an element at the specified position in this container with a given element.
527
528**System capability**: SystemCapability.Utils.Lang
529
530**Parameters**
531
532| Name| Type| Mandatory| Description|
533| -------- | -------- | -------- | -------- |
534| index | number | Yes| Position index of the target element.|
535| element | T | Yes| Element to be used for replacement.|
536
537**Return value**
538
539| Type| Description|
540| -------- | -------- |
541| T | New element.|
542
543**Example**
544
545```ts
546let linkedList = new LinkedList();
547linkedList.add(2);
548linkedList.add(4);
549linkedList.add(5);
550linkedList.add(4);
551let result = linkedList.set(2, "b");
552```
553
554### convertToArray
555
556convertToArray(): Array<T>
557
558Converts this container into an array.
559
560**System capability**: SystemCapability.Utils.Lang
561
562**Return value**
563
564| Type| Description|
565| -------- | -------- |
566| Array<T> | Array obtained.|
567
568**Example**
569```ts
570let linkedList = new LinkedList();
571linkedList.add(2);
572linkedList.add(4);
573linkedList.add(5);
574linkedList.add(4);
575let result = linkedList.convertToArray();
576```
577
578### getFirst
579
580getFirst(): T
581
582Obtains the first element in this container.
583
584**System capability**: SystemCapability.Utils.Lang
585
586**Return value**
587
588| Type| Description|
589| -------- | -------- |
590| T | Returns the element if obtained; returns **undefined** otherwise.|
591
592**Example**
593
594```ts
595let linkedList = new LinkedList();
596linkedList.add(2);
597linkedList.add(4);
598linkedList.add(5);
599linkedList.add(4);
600let result = linkedList.getFirst();
601```
602
603### getLast
604
605getLast(): T
606
607Obtains the last element in this container.
608
609**System capability**: SystemCapability.Utils.Lang
610
611**Return value**
612
613| Type| Description|
614| -------- | -------- |
615| T | Returns the element if obtained; returns **undefined** otherwise.|
616
617**Example**
618
619```ts
620let linkedList = new LinkedList();
621linkedList.add(2);
622linkedList.add(4);
623linkedList.add(5);
624linkedList.add(4);
625linkedList.getLast();
626```
627
628### [Symbol.iterator]
629
630[Symbol.iterator]\(): IterableIterator<T>
631
632Obtains an iterator, each item of which is a JavaScript object.
633
634**System capability**: SystemCapability.Utils.Lang
635
636**Return value**
637
638| Type| Description|
639| -------- | -------- |
640| IterableIterator<T> | Iterator obtained.|
641
642**Example**
643
644```ts
645let linkedList = new LinkedList();
646linkedList.add(2);
647linkedList.add(4);
648linkedList.add(5);
649linkedList.add(4);
650
651// Method 1:
652for (let item of linkedList) {
653  console.log("value:" + item);
654}
655
656// Method 2:
657let iter = linkedList[Symbol.iterator]();
658let temp = iter.next().value;
659while(temp != undefined) {
660  console.log("value:" + temp);
661  temp = iter.next().value;
662}
663```
664