Singly linked list: insert delete efficient than an array
// Create the node the Node class the Node { constructor (Data) { the this .data = Data; the this .next = null ; } } class List { constructor () { // define a called attribute header points to the first node the this .head = null ; // length of the this .length = 0 ; } // append node the append (Element) { // the value is changed to the node the let the newNode = new new the node (Element); // determines whether the list is empty iF ( this.head == null) { this.head = newNode; } else { let current = this.head; while (current.next != null) { // 如何将current后移 current = current.next } current.next = newNode; } // 长度必须加1 this.length++; } isEmpty() { if (this.head == null) { return true; } return false; } clear() { this.head = null this.length = 0 } size() { return this.length } toString(callBack) { let current = this.head; while (current) { callBack(current.data); current = current.next } } // 在任意位置添加一个节点/元素 insert(position, element) { // out of range determination if (position <0 || position> the this .length) { return to false ; } // Create a new node the let the newNode = new new the Node (Element); the let Current = the this .head; // inserted in the head if (position == 0 ) { newNode.next = the this .head; the this .head = the newNode; } // inserted at the end of the else IF (== position the this .length) { // find the tail node the while (current.next =! Null ) { Current = current.next; } current.next = the newNode; } // inserted in the middle of the else { // find the correct position of the let index = 0 ; the while (index ++! position = -. 1 ) { Current = current.next; } newNode.next = current.next; current.next = the newNode; } the this .length ++ ; return to true ; } // in the current data element of index matching the indexOf (Element) { // traverse the list from the let Current = the this .head; // A method // for (the let index 0 =; index <this.length; index ++) { // // value list element in comparison with the extraction // IF (current.data === element) { // // returns the index is equal to // return index ; // } // // continue traversing unequal // Current = current.next; // } // method II index = 0 the let the while (Current) { // the value list element in comparison with the extraction IF (current.data === element) { // is equal to the subscript return return index; } // unequally continue traversing Current = Current .next; index ++ ; } // iterate is still not found after // return -1 return -1 ; } // node removes specific location // 0 removeAt (position) { IF (position <0 || position > = the this .length) return false; let current = this.head; // 删除头 if (position == 0) { this.head = this.head.next; } // 尾 else if (position == this.length - 1) { let previous = null while (current.next) { previous = current current = current.next; } previous.next = null ; } // intermediate the else { // find the previous node of the current node is set to be removed the let index = 0 the while (! = position index ++ -. 1 ) { Current = current.next } // the next current node points to the next node of the current node to the next node current.next = current.next.next } the this .length-- ; return to true ; } Remove (Element) { return the this .removeAt ( the this .indexOf (Element)); } }