• 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();
201let result1 = linkedList.has("squirrel");
202linkedList.add("squirrel");
203let result = linkedList.has("squirrel");
204```
205
206### get
207
208get(index: number): T
209
210Obtains an element at the specified position in this container.
211
212**System capability**: SystemCapability.Utils.Lang
213
214**Parameters**
215
216| Name| Type| Mandatory| Description|
217| -------- | -------- | -------- | -------- |
218| index | number | Yes| Position index of the target element.|
219
220**Return value**
221
222| Type| Description|
223| -------- | -------- |
224| T | Element obtained.|
225
226**Error codes**
227
228For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
229
230| ID| Error Message|
231| -------- | -------- |
232| 10200011 | The get method cannot be bound. |
233
234**Example**
235
236```ts
237let linkedList = new LinkedList();
238linkedList.add(2);
239linkedList.add(4);
240linkedList.add(5);
241linkedList.add(2);
242linkedList.add(1);
243linkedList.add(2);
244linkedList.add(4);
245let result = linkedList.get(2);
246```
247
248### getLastIndexOf
249
250getLastIndexOf(element: T): number
251
252Obtains the index of the last occurrence of the specified element in this container.
253
254**System capability**: SystemCapability.Utils.Lang
255
256**Parameters**
257
258| Name| Type| Mandatory| Description|
259| -------- | -------- | -------- | -------- |
260| element | T | Yes| Target element.|
261
262**Return value**
263
264| Type| Description|
265| -------- | -------- |
266| number | Returns the position index if obtained; returns **-1** otherwise.|
267
268**Error codes**
269
270For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
271
272| ID| Error Message|
273| -------- | -------- |
274| 10200011 | The getLastIndexOf method cannot be bound. |
275
276**Example**
277
278```ts
279let linkedList = new LinkedList();
280linkedList.add(2);
281linkedList.add(4);
282linkedList.add(5);
283linkedList.add(2);
284linkedList.add(1);
285linkedList.add(2);
286linkedList.add(4);
287let result = linkedList.getLastIndexOf(2);
288```
289
290### getIndexOf
291
292getIndexOf(element: T): number
293
294Obtains the index of the first occurrence of the specified element in this container.
295
296**System capability**: SystemCapability.Utils.Lang
297
298**Parameters**
299
300| Name| Type| Mandatory| Description|
301| -------- | -------- | -------- | -------- |
302| element | T | Yes| Target element.|
303
304**Return value**
305
306| Type| Description|
307| -------- | -------- |
308| number | Returns the position index if obtained; returns **-1** otherwise.|
309
310**Error codes**
311
312For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
313
314| ID| Error Message|
315| -------- | -------- |
316| 10200011 | The getIndexOf method cannot be bound. |
317
318**Example**
319
320```ts
321let linkedList = new LinkedList();
322linkedList.add(2);
323linkedList.add(4);
324linkedList.add(5);
325linkedList.add(2);
326linkedList.add(1);
327linkedList.add(2);
328linkedList.add(4);
329let result = linkedList.getIndexOf(2);
330```
331
332### removeByIndex
333
334removeByIndex(index: number): T
335
336Removes an element at the specified position from this container.
337
338**System capability**: SystemCapability.Utils.Lang
339
340**Parameters**
341
342| Name| Type| Mandatory| Description|
343| -------- | -------- | -------- | -------- |
344| index | number | Yes| Position index of the target element.|
345
346**Return value**
347
348| Type| Description|
349| -------- | -------- |
350| T | Element removed.|
351
352**Error codes**
353
354For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
355
356| ID| Error Message|
357| -------- | -------- |
358| 10200011 | The removeByIndex method cannot be bound. |
359| 10200001 | The value of index is out of range. |
360
361**Example**
362
363```ts
364let linkedList = new LinkedList();
365linkedList.add(2);
366linkedList.add(4);
367linkedList.add(5);
368linkedList.add(2);
369linkedList.add(4);
370let result = linkedList.removeByIndex(2);
371```
372
373### removeFirst
374
375removeFirst(): T
376
377Removes the first element from this container.
378
379**System capability**: SystemCapability.Utils.Lang
380
381**Return value**
382
383| Type| Description|
384| -------- | -------- |
385| T | Element removed.|
386
387**Error codes**
388
389For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
390
391| ID| Error Message|
392| -------- | -------- |
393| 10200011 | The removeFirst method cannot be bound. |
394| 10200010 | Container is empty. |
395
396**Example**
397
398```ts
399let linkedList = new LinkedList();
400linkedList.add(2);
401linkedList.add(4);
402linkedList.add(5);
403linkedList.add(2);
404linkedList.add(4);
405let result = linkedList.removeFirst();
406```
407
408### removeLast
409
410removeLast(): T
411
412Removes the last element from this container.
413
414**System capability**: SystemCapability.Utils.Lang
415
416**Return value**
417
418| Type| Description|
419| -------- | -------- |
420| T | Element removed.|
421
422**Error codes**
423
424For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
425
426| ID| Error Message|
427| -------- | -------- |
428| 10200011 | The removeLast method cannot be bound. |
429| 10200010 | Container is empty. |
430
431**Example**
432
433```ts
434let linkedList = new LinkedList();
435linkedList.add(2);
436linkedList.add(4);
437linkedList.add(5);
438linkedList.add(2);
439linkedList.add(4);
440let result = linkedList.removeLast();
441```
442
443### remove
444
445remove(element: T): boolean
446
447Removes the first occurrence of the specified element from this container.
448
449**System capability**: SystemCapability.Utils.Lang
450
451**Parameters**
452
453| Name| Type| Mandatory| Description|
454| -------- | -------- | -------- | -------- |
455| element | T | Yes| Target element.|
456
457**Return value**
458
459| Type| Description|
460| -------- | -------- |
461| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
462
463**Error codes**
464
465For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
466
467| ID| Error Message|
468| -------- | -------- |
469| 10200011 | The remove method cannot be bound. |
470
471**Example**
472
473```ts
474let linkedList = new LinkedList();
475linkedList.add(2);
476linkedList.add(4);
477linkedList.add(5);
478linkedList.add(4);
479let result = linkedList.remove(2);
480```
481
482### removeFirstFound
483
484removeFirstFound(element: T): boolean
485
486Removes the first occurrence of the specified element from this container.
487
488**System capability**: SystemCapability.Utils.Lang
489
490**Parameters**
491
492| Name| Type| Mandatory| Description|
493| -------- | -------- | -------- | -------- |
494| element | T | Yes| Target element.|
495
496**Return value**
497
498| Type| Description|
499| -------- | -------- |
500| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
501
502**Error codes**
503
504For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
505
506| ID| Error Message|
507| -------- | -------- |
508| 10200011 | The removeFirstFound method cannot be bound. |
509| 10200010 | Container is empty. |
510| 10200017 | The element does not exist in this container. |
511
512**Example**
513
514```ts
515let linkedList = new LinkedList();
516linkedList.add(2);
517linkedList.add(4);
518linkedList.add(5);
519linkedList.add(4);
520let result = linkedList.removeFirstFound(4);
521```
522
523### removeLastFound
524
525removeLastFound(element: T): boolean
526
527Removes the last occurrence of the specified element from this container.
528
529**System capability**: SystemCapability.Utils.Lang
530
531**Parameters**
532
533| Name| Type| Mandatory| Description|
534| -------- | -------- | -------- | -------- |
535| element | T | Yes| Target element.|
536
537**Return value**
538
539| Type| Description|
540| -------- | -------- |
541| boolean | Returns **true** if the element is removed successfully; returns **false** otherwise.|
542
543**Error codes**
544
545For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
546
547| ID| Error Message|
548| -------- | -------- |
549| 10200011 | The removeLastFound method cannot be bound. |
550| 10200010 | Container is empty. |
551| 10200017 | The element does not exist in this container. |
552
553**Example**
554
555```ts
556let linkedList = new LinkedList();
557linkedList.add(2);
558linkedList.add(4);
559linkedList.add(5);
560linkedList.add(4);
561let result = linkedList.removeLastFound(4);
562```
563
564### clone
565
566clone(): LinkedList<T>
567
568Clones this container and returns a copy. The modification to the copy does not affect the original instance.
569
570**System capability**: SystemCapability.Utils.Lang
571
572**Return value**
573
574| Type| Description|
575| -------- | -------- |
576| LinkedList<T> | New **LinkedList** instance obtained.|
577
578**Error codes**
579
580For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
581
582| ID| Error Message|
583| -------- | -------- |
584| 10200011 | The clone method cannot be bound. |
585
586**Example**
587
588```ts
589let linkedList = new LinkedList();
590linkedList.add(2);
591linkedList.add(4);
592linkedList.add(5);
593linkedList.add(4);
594let result = linkedList.clone();
595```
596
597### forEach
598
599forEach(callbackFn: (value: T, index?: number, LinkedList?: LinkedList<T>) => void,
600thisArg?: Object): void
601
602Uses a callback to traverse the elements in this container and obtain their position indexes.
603
604**System capability**: SystemCapability.Utils.Lang
605
606**Parameters**
607
608| Name| Type| Mandatory| Description|
609| -------- | -------- | -------- | -------- |
610| callbackFn | function | Yes| Callback invoked to traverse the elements in the container.|
611| thisArg | Object | No| Value to use when the callback is invoked.|
612
613callbackfn
614
615| Name| Type| Mandatory| Description|
616| -------- | -------- | -------- | -------- |
617| value | T | Yes| Value of the element that is currently traversed.|
618| index | number | No| Position index of the element that is currently traversed.|
619| LinkedList | LinkedList<T> | No| Instance that invokes the **forEach** API.|
620
621**Error codes**
622
623For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
624
625| ID| Error Message|
626| -------- | -------- |
627| 10200011 | The forEach method cannot be bound. |
628
629**Example**
630
631```ts
632let linkedList = new LinkedList();
633linkedList.add(2);
634linkedList.add(4);
635linkedList.add(5);
636linkedList.add(4);
637linkedList.forEach((value, index) => {
638    console.log("value:" + value, "index:" + index);
639});
640```
641
642### clear
643
644clear(): void
645
646Clears this container and sets its length to **0**.
647
648**System capability**: SystemCapability.Utils.Lang
649
650**Error codes**
651
652For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
653
654| ID| Error Message|
655| -------- | -------- |
656| 10200011 | The clear method cannot be bound. |
657
658**Example**
659
660```ts
661let linkedList = new LinkedList();
662linkedList.add(2);
663linkedList.add(4);
664linkedList.add(5);
665linkedList.add(4);
666linkedList.clear();
667```
668
669### set
670
671set(index: number, element: T): T
672
673Replaces an element at the specified position in this container with a given element.
674
675**System capability**: SystemCapability.Utils.Lang
676
677**Parameters**
678
679| Name| Type| Mandatory| Description|
680| -------- | -------- | -------- | -------- |
681| index | number | Yes| Position index of the target element.|
682| element | T | Yes| Element to be used for replacement.|
683
684**Return value**
685
686| Type| Description|
687| -------- | -------- |
688| T | New element.|
689
690**Error codes**
691
692For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
693
694| ID| Error Message|
695| -------- | -------- |
696| 10200011 | The set method cannot be bound. |
697| 10200001 | The value of index is out of range. |
698
699**Example**
700
701```ts
702let linkedList = new LinkedList();
703linkedList.add(2);
704linkedList.add(4);
705linkedList.add(5);
706linkedList.add(4);
707let result = linkedList.set(2, "b");
708```
709
710### convertToArray
711
712convertToArray(): Array<T>
713
714Converts this container into an array.
715
716**System capability**: SystemCapability.Utils.Lang
717
718**Return value**
719
720| Type| Description|
721| -------- | -------- |
722| Array<T> | Array obtained.|
723
724**Error codes**
725
726For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
727
728| ID| Error Message|
729| -------- | -------- |
730| 10200011 | The convertToArray method cannot be bound. |
731
732**Example**
733```ts
734let linkedList = new LinkedList();
735linkedList.add(2);
736linkedList.add(4);
737linkedList.add(5);
738linkedList.add(4);
739let result = linkedList.convertToArray();
740```
741
742### getFirst
743
744getFirst(): T
745
746Obtains the first element in this container.
747
748**System capability**: SystemCapability.Utils.Lang
749
750**Return value**
751
752| Type| Description|
753| -------- | -------- |
754| T | Returns the element if obtained; returns **undefined** otherwise.|
755
756**Error codes**
757
758For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
759
760| ID| Error Message|
761| -------- | -------- |
762| 10200011 | The getFirst method cannot be bound. |
763
764**Example**
765
766```ts
767let linkedList = new LinkedList();
768linkedList.add(2);
769linkedList.add(4);
770linkedList.add(5);
771linkedList.add(4);
772let result = linkedList.getFirst();
773```
774
775### getLast
776
777getLast(): T
778
779Obtains the last element in this container.
780
781**System capability**: SystemCapability.Utils.Lang
782
783**Return value**
784
785| Type| Description|
786| -------- | -------- |
787| T | Returns the element if obtained; returns **undefined** otherwise.|
788
789**Error codes**
790
791For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
792
793| ID| Error Message|
794| -------- | -------- |
795| 10200011 | The getLast method cannot be bound. |
796
797**Example**
798
799```ts
800let linkedList = new LinkedList();
801linkedList.add(2);
802linkedList.add(4);
803linkedList.add(5);
804linkedList.add(4);
805linkedList.getLast();
806```
807
808### [Symbol.iterator]
809
810[Symbol.iterator]\(): IterableIterator<T>
811
812Obtains an iterator, each item of which is a JavaScript object.
813
814**System capability**: SystemCapability.Utils.Lang
815
816**Return value**
817
818| Type| Description|
819| -------- | -------- |
820| IterableIterator<T> | Iterator obtained.|
821
822**Error codes**
823
824For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md).
825
826| ID| Error Message|
827| -------- | -------- |
828| 10200011 | The Symbol.iterator method cannot be bound. |
829
830**Example**
831
832```ts
833let linkedList = new LinkedList();
834linkedList.add(2);
835linkedList.add(4);
836linkedList.add(5);
837linkedList.add(4);
838
839// Method 1:
840for (let item of linkedList) {
841  console.log("value:" + item);
842}
843
844// Method 2:
845let iter = linkedList[Symbol.iterator]();
846let temp = iter.next().value;
847while(temp != undefined) {
848  console.log("value:" + temp);
849  temp = iter.next().value;
850}
851```
852