How to use JavaScript to implement complete single-chain table detailed

Preface

The front end must also do a good job in the data structure!

With JavaScript to implement a single-link list, instantiate a single-chain table data structure with a LINKEDLIST constructor, all methods are placed on the prototype object of the LinkedList constructor, written all methods that temporarily think of

GitHub source code address, download can be run

Create a linked list instance through the LINKEDLIST class, add additions to the list , search, delete, display nodes and other methods
lists the initial default have a “_head” head node, hidden

when using
    by element / index to add, delete, return an error is found, lookup returns null or -1
  • not found
  • let obj = new LinkedList ()
  • method description

】 Find
Obj.Find (item)

Finding the element

Obj.FindIndex (INDEX)

Find the index through the index to the element
  • obj.findIndexOf (item) to find the index of the element by element content item
  • obj.findPrev (item ) to find a node on the item element by element
  • Add
  • obj.insert (item, newElement)
Inserting new elements after ITEM elements

Obj.push (item)

  • Obj.InsertIndex (INDEEMENT)
  • in the Index index
OBJ.Remove (Item)

Delete Item element

  • Obj.removeIndex (Index) Delete Index Index Node
  • Obj.Size () Returns the length of the linked list

OBJ.DISPLAY ()

The array is returned to the list, which is easy to observe, test
  • Obj.reversal () Link list sequence reversal (recursive)
  • Method code
  • Link table LINKEDLIST

Function LinkedList (… rest) {this._head = new node (‘ _Head ‘) // Link table header node // If there is a pass value, add it to an instance if (rest [0],’ _head ‘) for (let i = 1; i

Create a new node Node

  function node (element) {this.Element = Element this.next = NULL} < rest.length; i++) {
 this.insert(rest[i], rest[i - 1])
 }
 }
 }
 LinkedList.prototype.find = find
 LinkedList.prototype.findPrev = findPrev
 LinkedList.prototype.findIndex = findIndex
 LinkedList.prototype.findIndexOf = findIndexOf
 LinkedList.prototype.push = push
 LinkedList.prototype.insert = insert
 LinkedList.prototype.insertIndex = insertIndex
 LinkedList.prototype.remove = remove
 LinkedList.prototype.removeIndex = removeIndex
 LinkedList.prototype.size = size
 LinkedList.prototype.display = display
 LinkedList.prototype.reversal = reversal 
Obj.Find (item)

/ / Find function, look up the location of Item in the linked list, and put It returns, did not find the return -1 function find (item) {let currnode = this._head while (currnode! == null && currnode.ement! == item) {currnode = currnode.next} if (currnode! == null ) {return currnode}}

   
// passed Element Index Returns the element Function Findex (INDEX) {let currnode = 0 while (currnode! == null) {// find the index location, return the current node, go out of the head node if (tmpIndex = == Index + 1) {return currnode} tmpindex + = 1 currnode = currnode.next} return null}

 Obj.FindIndexof (item)   
Function Findexof (item) {let currnode = this._head let tmpindex =0 while (currnode.next! == null && currnode.next.Element! == iTEM) {tmpindex + = 1 currnode = currnode.next} {return tmpinDex} else {return -1} else {return }

Obj.FindPrev (item)
  // Look for the last node of the target node item, no found return -1 function findPrev (item) {let currNode = this._head while (currNode.next! == null && currNode.next.element! == item) {currNode = currNode.next} if (currNode.next! == Item) {return currnode}  

Obj.insert (item, newElement)

  // Insert the node, find the node location you want to insert, plug in the new node into the back of item (newElement, item) {let need = new node (newElement) let currnode = this.find (item) if (Currnode ) {newNode.next = currnode.next currnode.Next = newNode} else {console.error (`INSERT ERROR: There is no" $ {item} "node in the linked list)}  

Obj.insertIndex (INDEX, NEWELEMENT)

// Insert the node, the new node is inserted into the index index Function Insertex (NewElement, Index) {let’s = new node (newElement) let currNode = this. FindIndex (index) {newnot.next = currnode.next currnode.next = newNode} else {console.error (`InsertIndEx error: There is no” $ {index} “index node in the list)}}
   Obj.push (item) 
/ / In the last bit of the chain table Function Push (Element) {let New Node (Element) let currnode = this._head while (currnode.next! == null) {currnode = currnode.next} currnode.next = newNode}

obj.remove (item)
   / / Delete node, find deletionIn addition, deletion, no prompt error function remove (item) {// find the current and last node, let the next node's next point to the next node Let TMPPREV = this.FindPrev (item) let tmpnext = this. Find (item) if (tmprev.next = tmpnext.next} else {console.error (`Remove Error: There is no" $ {item} "node in the list)}} 

Obj.removeIndex (Index)

// Remove Node Function RemoveIndex (INDEX) {Let TMPPREV = THIS under an index. findIndex (index – 1) let currNode = this.findIndex (index) if (tmpPrev && currNode) {tmpPrev.next = currNode.next} else {console.error ( `removeIndex error: not present in the list” $ {index} ” Index node `)}}
}   

function size () {let currnode = THIS ._head let tmpsize = 0 while (currnode.next! == null) {tmpsize + = 1 currnode = currnode.NEXT} return tmpsize // No head node}

   
// Link list reverse => recursive function reversal () {function reversalList (item) {if (item.next) {let tmpItem = reversalList (item.next) item.next = null tmpItem.next = item return item} else {obj._head .next = item returni}}}}} Reversallist (Obj._Head.Next)}

Obj.display ()
   Function Display () {// Link list display and use, the default head does not exist let currnode = this._Head.Next let tmparr = [] while (currnode! == null) {TMPARR.PUSH (currnode) currnode = currnode 

Example test

   // run test let Obj = new linkedList ('Node 0', 'Node 1', 'Node 2', 'Node 3', 'Node 4', 'Node 5') Console.log ('--- Instance Object') COnsole.log (OBJ) Console.log ('--- Late Insert Element') Obj.push ('Push Insert') Console.log (Obj.display ()) Console.log ('--- Insert Elements) ') Obj.insert (' element insert ", 'node 2') console.log (Obj.display ()) Console.log (inserts element ') obj.insertIndex (' index insert", 5 Console.log (Obj.display ()) Console.log ('- looking for element location') console.log (Obj.Find ('Node 4')) Console.log ('--- Remove Element " ) obj.remove ('Node 5') console.log (Obj.display ()) Console.log ('--- Remove Index Elements') Obj.RemoveIndex (5) Console.log (Obj.display ()) Console.log ('--- Element Length') console.log (Obj.Size ()) console.log ('--- index ") console.log (Obj.FindIndex (2)) console.log --- Element Find Index ') Console.log (Obj.FindIndexof (' Node 3 ')) Console.log (' --- Inverted List "Obj.Reversal () Console.log (Obj.display ()) 

 end   
Recently, I have encountered a problem with the single-link table, allA single-chain table reversal method, using recursive

Several methods of implementing single-link tables
   Summary 
The above is the full content of this article, I hope this paper has a certain reference value for everyone’s learning or work. If you have any questions, you can leave a message, thank you Support for TUMI clouds.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment