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