The size of the array is fixed, and it is expensive to insert or remove items from the beginning or middle of the array (other elements need to be moved); Linked lists do not need to move other elements when adding or removing elements. Linked lists store an ordered collection of elements, which are not placed contiguous in memory, each of which consists of a node that stores itself and a reference to the next node element; Arrays are placed consecutively in memory. The array can directly access the elements at any location, and the access time complexity of the array elements iso(1);A linked list needs to iterate through the list from the beginning (header) until the desired element is found, and the access time complexity of the linked list element iso(n)。append(item): Adds a new element to the end of the listinsert (position,item): Inserts an element to a specific position in the listRemove(item): Removes an element from the linked listindexof(item): Returns the index of the element in the linked listRemoveat(position): Removes the element from the linked list at the specified position isempty(): Returns if there are no elements in the linked list true, otherwise falsesize(): returns the number of elements in the linked listtostring(): outputs the value and index of the elementfunction linklist() let head=null,length=0; this.append=function(item)else current.next=node } length++ this.insert=function(position,item)else current=current.next index++ console.log(`index of $-s`,-1) return -1 } this.removeat=function(position) }class es6linklist append(item)else current.next=node } this.length++ insert(pos,item)else{ while(index++=0 &&index=0 &&&pos The linked list is null true
The element 2 you are looking for is in position 2 in the linked list
Element: 7 - Index: 0 |Element: 1 - Index: 1 |Element: 2 - Index: 2 |Element: 9 - Index: 3 |
The linked list is 4 in length
Element: 7 - Index: 0 |Element: 1 - Index: 1 |Element: 9 - Index: 2 |
The linked list is 2 in length
Whether the linked list is empty false
Element: 1--Index: 0|Element: 9 - Index: 1 |
The position bit of element 7 you are looking for in the linked list does not exist.