I recently read the book "JavaScript data structures and algorithms" to explain the knowledge of the javascript Data Structure linked list. I think this is a short board.
Linked List: stores ordered element sets, but unlike arrays, elements in the linked list are not placed continuously in the memory. Each element consists of a node that stores the element itself and a reference (also called a pointer or link) pointing to the next element.
Benefit: You can add or remove any item. It will expand as needed without moving other elements.
Differences from Arrays:
Array: You can directly access any element at any location;
Linked List: to access an element in the linked list, You Need To iterate the list from the starting point (header) until you find the required element.
Take notes.
Function compute list () {var Node = function (element) {this. element = elementthis. next = null} var length = 0var head = nullthis. append = function (element) {var node = new Node (element) var currentif (head = null) {// The linked list is empty head = node} else {// The linked list is not empty current = head // The linked list loop until the last while (current. next) {current = current. next} current. next = node} length ++ // update the length of the linked list} this. insert = function (position, element) {var node = new Node (element) var current = headvar previusvar index = 0if (position> = 1 & position <= length) {// determine whether to cross-border if (position = 0) {// Insert the first node. next = currenthead = node} else {while (index ++ <position) {previous = currentcurrent = current. next} node. next = currentprevious. next = node} length ++ // update the linked list length return true} else {return false} this. indexOf = function (element) {var current = headvar index =-1 while (current) {if (element = current. element) {return index} index ++ current = current. next} return-1} this. removeAt = function (position) {if (position>-1 & position