Recently looking at "JavaScript data structure and algorithms" This book, to fill the data structure and algorithm part of the knowledge, feel that this piece is a short plate.
List : stores an ordered set of elements, but unlike arrays, the elements in a list are not contiguous in memory. Each element consists of a node of the storage element itself and a reference (also known as a pointer or link) to the next element.
Benefits: You can add or remove any item, and it expands on demand and does not need to move other elements.
The difference from the array:
Array: Any element that can be accessed directly from any location;
Linked lists: To access an element in a linked list, you need to start iterating through the list from the start point (the header) until you find the element you want.
Make some small notes.
function LinkedList () {var Node = function (Element) {this.element = element This.next = Nu ll} var length = 0 var head = null This.append = function (element) {var node = new node (element) var current if (head = = n ULL) {//linked list is empty head = node}else{//list is not empty current = head//loop list until last while (current.next) {current = Current.next} current . Next = node} length + +//update linked list lengths} This.insert = function (position,element) {var node = new node (element) var current = h EAD var previous var index = 0 if (position>=1 && position<=length) {//To determine whether cross-border if (position = 0) {//Insert header node . Next = Current head = node}else{while (index++ < position) {Previous = current current = Current.next} Node.next = C Urrent previous.next = node} length + +//update linked list Len true}else{return False}} This.indexof = function (Element) {var Current = head var index =-1 while (current) {if (element = = current.element) {return index} index++ . Next} Return-1} this. RemoveAt = function (position) {if (position>-1 && position<length) {//To determine whether the bounds of var current = head Var previous var index = 0 if (position = 0) {//Remove first element head = Current.next}else{while (index++ < position) {previous = current C
urrent = current.next} Previous.next = current.next//removal element} Length--//Update length return current.element}else{return null} } This.remove = function (Element) {var index = This.indexof (Element) return This.removeat (index)} this.isempty = Functio N () {return length = = 0} this.size = function () {return length} this.tostring = function () {var current = head Var Strin g = "" while [current] {string = "," + current.element current = Current.next} return String.slice (1)} This.gethead = Fun Ction () {return head}}
The above is a small set to introduce the JavaScript data structure of the list of knowledge, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!