• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.util.LinkedList (Linear Container LinkedList)
2
3**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.
4
5Unlike **[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.
6
7**LinkedList** is more efficient in data insertion than **[ArrayList](js-apis-arraylist.md)**, but less efficient in data access.
8
9**Recommended use case**: Use **LinkedList** for frequent insertion and removal operations.
10
11This topic uses the following to identify the use of generics:
12- T: Type
13
14> **NOTE**
15>
16> 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.
17
18
19## Modules to Import
20
21```ts
22import LinkedList from '@ohos.util.LinkedList';
23```
24
25## LinkedList
26
27### Attributes
28
29**System capability**: SystemCapability.Utils.Lang
30
31| Name| Type| Readable| Writable| Description|
32| -------- | -------- | -------- | -------- | -------- |
33| length | number | Yes| No| Number of elements in a linked list (called container later).|
34
35
36### constructor
37
38constructor()
39
40A constructor used to create a **LinkedList** instance.
41
42**System capability**: SystemCapability.Utils.Lang
43
44**Error codes**
45
46For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
47
48| ID| Error Message|
49| -------- | -------- |
50| 10200012 | The LinkedList's constructor cannot be directly invoked. |
51
52
53**Example**
54
55```ts
56let linkedList = new LinkedList();
57```
58
59
60### add
61
62add(element: T): boolean
63
64Adds an element at the end of this container.
65
66**System capability**: SystemCapability.Utils.Lang
67
68**Parameters**
69
70| Name| Type| Mandatory| Description|
71| -------- | -------- | -------- | -------- |
72| element | T | Yes| Target element.|
73
74**Return value**
75
76| Type| Description|
77| -------- | -------- |
78| boolean | Returns **true** if the element is added successfully; returns **false** otherwise.|
79
80**Error codes**
81
82For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
83
84| ID| Error Message|
85| -------- | -------- |
86| 10200011 | The add method cannot be bound. |
87
88**Example**
89
90```ts
91let linkedList = new LinkedList();
92let result = linkedList.add("a");
93let result1 = linkedList.add(1);
94let b = [1, 2, 3];
95let result2 = linkedList.add(b);
96let c = {name : "Dylon", age : "13"};
97let result3 = linkedList.add(c);
98let result4 = linkedList.add(false);
99```
100
101### addFirst
102
103addFirst(element: T): void
104
105Adds an element at the top of this container.
106
107**System capability**: SystemCapability.Utils.Lang
108
109**Parameters**
110
111| Name| Type| Mandatory| Description|
112| -------- | -------- | -------- | -------- |
113| element | T | Yes| Target element.|
114
115**Error codes**
116
117For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
118
119| ID| Error Message|
120| -------- | -------- |
121| 10200011 | The addFirst method cannot be bound. |
122
123**Example**
124
125```ts
126let linkedList = new LinkedList();
127linkedList.addFirst("a");
128linkedList.addFirst(1);
129let b = [1, 2, 3];
130linkedList.addFirst(b);
131let c = {name : "Dylon", age : "13"};
132linkedList.addFirst(c);
133linkedList.addFirst(false);
134```
135
136### insert
137
138insert(index: number, element: T): void
139
140Inserts an element at the specified position in this container.
141
142**System capability**: SystemCapability.Utils.Lang
143
144**Parameters**
145
146| Name| Type| Mandatory| Description|
147| -------- | -------- | -------- | -------- |
148| element | T | Yes| Target element.|
149| index | number | Yes| Index of the position where the element is to be inserted.|
150
151**Error codes**
152
153For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
154
155| ID| Error Message|
156| -------- | -------- |
157| 10200011 | The insert method cannot be bound. |
158| 10200001 | The value of index is out of range. |
159
160**Example**
161
162```ts
163let linkedList = new LinkedList();
164linkedList.insert(0, "A");
165linkedList.insert(1, 0);
166linkedList.insert(2, true);
167```
168
169### has
170
171has(element: T): boolean
172
173Checks whether this container has the specified element.
174
175**System capability**: SystemCapability.Utils.Lang
176
177**Parameters**
178
179| Name| Type| Mandatory| Description|
180| -------- | -------- | -------- | -------- |
181| element | T | Yes| Target element.|
182
183**Return value**
184
185| Type| Description|
186| -------- | -------- |
187| boolean | Returns **true** if the specified element is contained; returns **false** otherwise.|
188
189**Error codes**
190
191For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
192
193| ID| Error Message|
194| -------- | -------- |
195| 10200011 | The has method cannot be bound. |
196
197**Example**
198
199```ts
200let linkedList = new LinkedList();
201linkedList.add("squirrel");
202let result = linkedList.has("squirrel");
203```
204
205### get
206
207get(index: number): T
208
209Obtains an element at the specified position in this container.
210
211**System capability**: SystemCapability.Utils.Lang
212
213**Parameters**
214
215| Name| Type| Mandatory| Description|
216| -------- | -------- | -------- | -------- |
217| index | number | Yes| Position index of the target element.|
218
219**Return value**
220
221| Type| Description|
222| -------- | -------- |
223| T | Element obtained.|
224
225**Error codes**
226
227For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
228
229| ID| Error Message|
230| -------- | -------- |
231| 10200011 | The get method cannot be bound. |
232
233**Example**
234
235```ts
236let linkedList = new LinkedList();
237linkedList.add(2);
238linkedList.add(4);
239linkedList.add(5);
240linkedList.add(2);
241linkedList.add(1);
242linkedList.add(2);
243linkedList.add(4);
244let result = linkedList.get(2);
245```
246
247### getLastIndexOf
248
249getLastIndexOf(element: T): number
250
251Obtains the index of the last occurrence of the specified element in this container.
252
253**System capability**: SystemCapability.Utils.Lang
254
255**Parameters**
256
257| Name| Type| Mandatory| Description|
258| -------- | -------- | -------- | -------- |
259| element | T | Yes| Target element.|
260
261**Return value**
262
263| Type| Description|
264| -------- | -------- |
265| number | Returns the position index if obtained; returns **-1** otherwise.|
266
267**Error codes**
268
269For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
270
271| ID| Error Message|
272| -------- | -------- |
273| 10200011 | The getLastIndexOf method cannot be bound. |
274
275**Example**
276
277```ts
278let linkedList = new LinkedList();
279linkedList.add(2);
280linkedList.add(4);
281linkedList.add(5);
282linkedList.add(2);
283linkedList.add(1);
284linkedList.add(2);
285linkedList.add(4);
286let result = linkedList.getLastIndexOf(2);
287```
288
289### getIndexOf
290
291getIndexOf(element: T): number
292
293Obtains the index of the first occurrence of the specified element in this container.
294
295**System capability**: SystemCapability.Utils.Lang
296
297**Parameters**
298
299| Name| Type| Mandatory| Description|
300| -------- | -------- | -------- | -------- |
301| element | T | Yes| Target element.|
302
303**Return value**
304
305| Type| Description|
306| -------- | -------- |
307| number | Returns the position index if obtained; returns **-1** otherwise.|
308
309**Error codes**
310
311For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
312
313| ID| Error Message|
314| -------- | -------- |
315| 10200011 | The getIndexOf method cannot be bound. |
316
317**Example**
318
319```ts
320let linkedList = new LinkedList();
321linkedList.add(2);
322linkedList.add(4);
323linkedList.add(5);
324linkedList.add(2);
325linkedList.add(1);
326linkedList.add(2);
327linkedList.add(4);
328let result = linkedList.getIndexOf(2);
329```
330
331### removeByIndex
332
333removeByIndex(index: number): T
334
335Removes an element at the specified position from this container.
336
337**System capability**: SystemCapability.Utils.Lang
338
339**Parameters**
340
341| Name| Type| Mandatory| Description|
342| -------- | -------- | -------- | -------- |
343| index | number | Yes| Position index of the target element.|
344
345**Return value**
346
347| Type| Description|
348| -------- | -------- |
349| T | Element removed.|
350
351**Error codes**
352
353For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
354
355| ID| Error Message|
356| -------- | -------- |
357| 10200011 | The removeByIndex method cannot be bound. |
358| 10200001 | The value of index is out of range. |
359
360**Example**
361
362```ts
363let linkedList = new LinkedList();
364linkedList.add(2);
365linkedList.add(4);
366linkedList.add(5);
367linkedList.add(2);
368linkedList.add(4);
369let result = linkedList.removeByIndex(2);
370```
371
372### removeFirst
373
374removeFirst(): T
375
376Removes the first element from this container.
377
378**System capability**: SystemCapability.Utils.Lang
379
380**Return value**
381
382| Type| Description|
383| -------- | -------- |
384| T | Element removed.|
385
386**Error codes**
387
388For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
389
390| ID| Error Message|
391| -------- | -------- |
392| 10200011 | The removeFirst method cannot be bound. |
393| 10200010 | Container is empty. |
394
395**Example**
396
397```ts
398let linkedList = new LinkedList();
399linkedList.add(2);
400linkedList.add(4);
401linkedList.add(5);
402linkedList.add(2);
403linkedList.add(4);
404let result = linkedList.removeFirst();
405```
406
407### removeLast
408
409removeLast(): T
410
411Removes the last element from this container.
412
413**System capability**: SystemCapability.Utils.Lang
414
415**Return value**
416
417| Type| Description|
418| -------- | -------- |
419| T | Element removed.|
420
421**Error codes**
422
423For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
424
425| ID| Error Message|
426| -------- | -------- |
427| 10200011 | The removeLast method cannot be bound. |
428| 10200010 | Container is empty. |
429
430**Example**
431
432```ts
433let linkedList = new LinkedList();
434linkedList.add(2);
435linkedList.add(4);
436linkedList.add(5);
437linkedList.add(2);
438linkedList.add(4);
439let result = linkedList.removeLast();
440```
441
442### remove
443
444remove(element: T): boolean
445
446Removes the first occurrence of the specified element from this container.
447
448**System capability**: SystemCapability.Utils.Lang
449
450**Parameters**
451
452| Name| Type| Mandatory| Description|
453| -------- | -------- | -------- | -------- |
454| element | T | Yes| Target element.|
455
456**Return value**
457
458| Type| Description|
459| -------- | -------- |
460| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
461
462**Error codes**
463
464For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
465
466| ID| Error Message|
467| -------- | -------- |
468| 10200011 | The remove method cannot be bound. |
469
470**Example**
471
472```ts
473let linkedList = new LinkedList();
474linkedList.add(2);
475linkedList.add(4);
476linkedList.add(5);
477linkedList.add(4);
478let result = linkedList.remove(2);
479```
480
481### removeFirstFound
482
483removeFirstFound(element: T): boolean
484
485Removes the first occurrence of the specified element from this container.
486
487**System capability**: SystemCapability.Utils.Lang
488
489**Parameters**
490
491| Name| Type| Mandatory| Description|
492| -------- | -------- | -------- | -------- |
493| element | T | Yes| Target element.|
494
495**Return value**
496
497| Type| Description|
498| -------- | -------- |
499| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
500
501**Error codes**
502
503For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
504
505| ID| Error Message|
506| -------- | -------- |
507| 10200011 | The removeFirstFound method cannot be bound. |
508| 10200010 | Container is empty. |
509| 10200017 | The element does not exist in this container. |
510
511**Example**
512
513```ts
514let linkedList = new LinkedList();
515linkedList.add(2);
516linkedList.add(4);
517linkedList.add(5);
518linkedList.add(4);
519let result = linkedList.removeFirstFound(4);
520```
521
522### removeLastFound
523
524removeLastFound(element: T): boolean
525
526Removes the last occurrence of the specified element from this container.
527
528**System capability**: SystemCapability.Utils.Lang
529
530**Parameters**
531
532| Name| Type| Mandatory| Description|
533| -------- | -------- | -------- | -------- |
534| element | T | Yes| Target element.|
535
536**Return value**
537
538| Type| Description|
539| -------- | -------- |
540| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
541
542**Error codes**
543
544For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
545
546| ID| Error Message|
547| -------- | -------- |
548| 10200011 | The removeLastFound method cannot be bound. |
549| 10200010 | Container is empty. |
550| 10200017 | The element does not exist in this container. |
551
552**Example**
553
554```ts
555let linkedList = new LinkedList();
556linkedList.add(2);
557linkedList.add(4);
558linkedList.add(5);
559linkedList.add(4);
560let result = linkedList.removeLastFound(4);
561```
562
563### clone
564
565clone(): LinkedList<T>
566
567Clones this container and returns a copy. The modification to the copy does not affect the original instance.
568
569**System capability**: SystemCapability.Utils.Lang
570
571**Return value**
572
573| Type| Description|
574| -------- | -------- |
575| LinkedList<T> | New **LinkedList** instance obtained.|
576
577**Error codes**
578
579For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
580
581| ID| Error Message|
582| -------- | -------- |
583| 10200011 | The clone method cannot be bound. |
584
585**Example**
586
587```ts
588let linkedList = new LinkedList();
589linkedList.add(2);
590linkedList.add(4);
591linkedList.add(5);
592linkedList.add(4);
593let result = linkedList.clone();
594```
595
596### forEach
597
598forEach(callbackFn: (value: T, index?: number, LinkedList?: LinkedList<T>) => void,
599thisArg?: Object): void
600
601Uses a callback to traverse the elements in this container and obtain their position indexes.
602
603**System capability**: SystemCapability.Utils.Lang
604
605**Parameters**
606
607| Name| Type| Mandatory| Description|
608| -------- | -------- | -------- | -------- |
609| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
610| thisArg | Object | No| Value to use when the callback is invoked.|
611
612callbackfn
613
614| Name| Type| Mandatory| Description|
615| -------- | -------- | -------- | -------- |
616| value | T | Yes| Value of the element that is currently traversed.|
617| index | number | No| Position index of the element that is currently traversed.|
618| LinkedList | LinkedList<T> | No| Instance that invokes the **forEach** API.|
619
620**Error codes**
621
622For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
623
624| ID| Error Message|
625| -------- | -------- |
626| 10200011 | The forEach method cannot be bound. |
627
628**Example**
629
630```ts
631let linkedList = new LinkedList();
632linkedList.add(2);
633linkedList.add(4);
634linkedList.add(5);
635linkedList.add(4);
636linkedList.forEach((value, index) => {
637    console.log("value:" + value, "index:" + index);
638});
639```
640
641### clear
642
643clear(): void
644
645Clears this container and sets its length to **0**.
646
647**System capability**: SystemCapability.Utils.Lang
648
649**Error codes**
650
651For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
652
653| ID| Error Message|
654| -------- | -------- |
655| 10200011 | The clear method cannot be bound. |
656
657**Example**
658
659```ts
660let linkedList = new LinkedList();
661linkedList.add(2);
662linkedList.add(4);
663linkedList.add(5);
664linkedList.add(4);
665linkedList.clear();
666```
667
668### set
669
670set(index: number, element: T): T
671
672Replaces an element at the specified position in this container with a given element.
673
674**System capability**: SystemCapability.Utils.Lang
675
676**Parameters**
677
678| Name| Type| Mandatory| Description|
679| -------- | -------- | -------- | -------- |
680| index | number | Yes| Position index of the target element.|
681| element | T | Yes| Element to be used for replacement.|
682
683**Return value**
684
685| Type| Description|
686| -------- | -------- |
687| T | New element.|
688
689**Error codes**
690
691For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
692
693| ID| Error Message|
694| -------- | -------- |
695| 10200011 | The set method cannot be bound. |
696| 10200001 | The value of index is out of range. |
697
698**Example**
699
700```ts
701let linkedList = new LinkedList();
702linkedList.add(2);
703linkedList.add(4);
704linkedList.add(5);
705linkedList.add(4);
706let result = linkedList.set(2, "b");
707```
708
709### convertToArray
710
711convertToArray(): Array<T>
712
713Converts this container into an array.
714
715**System capability**: SystemCapability.Utils.Lang
716
717**Return value**
718
719| Type| Description|
720| -------- | -------- |
721| Array<T> | Array obtained.|
722
723**Error codes**
724
725For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
726
727| ID| Error Message|
728| -------- | -------- |
729| 10200011 | The convertToArray method cannot be bound. |
730
731**Example**
732```ts
733let linkedList = new LinkedList();
734linkedList.add(2);
735linkedList.add(4);
736linkedList.add(5);
737linkedList.add(4);
738let result = linkedList.convertToArray();
739```
740
741### getFirst
742
743getFirst(): T
744
745Obtains the first element in this container.
746
747**System capability**: SystemCapability.Utils.Lang
748
749**Return value**
750
751| Type| Description|
752| -------- | -------- |
753| T | Returns the element if obtained; returns **undefined** otherwise.|
754
755**Error codes**
756
757For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
758
759| ID| Error Message|
760| -------- | -------- |
761| 10200011 | The getFirst method cannot be bound. |
762
763**Example**
764
765```ts
766let linkedList = new LinkedList();
767linkedList.add(2);
768linkedList.add(4);
769linkedList.add(5);
770linkedList.add(4);
771let result = linkedList.getFirst();
772```
773
774### getLast
775
776getLast(): T
777
778Obtains the last element in this container.
779
780**System capability**: SystemCapability.Utils.Lang
781
782**Return value**
783
784| Type| Description|
785| -------- | -------- |
786| T | Returns the element if obtained; returns **undefined** otherwise.|
787
788**Error codes**
789
790For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
791
792| ID| Error Message|
793| -------- | -------- |
794| 10200011 | The getLast method cannot be bound. |
795
796**Example**
797
798```ts
799let linkedList = new LinkedList();
800linkedList.add(2);
801linkedList.add(4);
802linkedList.add(5);
803linkedList.add(4);
804let result = linkedList.getLast();
805```
806
807### [Symbol.iterator]
808
809[Symbol.iterator]\(): IterableIterator<T>
810
811Obtains an iterator, each item of which is a JavaScript object.
812
813**System capability**: SystemCapability.Utils.Lang
814
815**Return value**
816
817| Type| Description|
818| -------- | -------- |
819| IterableIterator<T> | Iterator obtained.|
820
821**Error codes**
822
823For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
824
825| ID| Error Message|
826| -------- | -------- |
827| 10200011 | The Symbol.iterator method cannot be bound. |
828
829**Example**
830
831```ts
832let linkedList = new LinkedList();
833linkedList.add(2);
834linkedList.add(4);
835linkedList.add(5);
836linkedList.add(4);
837
838// Method 1:
839for (let item of linkedList) {
840  console.log("value:" + item);
841}
842
843// Method 2:
844let iter = linkedList[Symbol.iterator]();
845let temp = iter.next().value;
846while(temp != undefined) {
847  console.log("value:" + temp);
848  temp = iter.next().value;
849}
850```
851