Sample example of two ordered chain table algorithms implemented by JS implementation

The instance of this paper tells the two ordered list algorithms implemented by JS implementation. Share to everyone for your reference, as follows:

combines two ordered layers into a new ordered list and returns. The new list is composed of all the nodes of the two linked lists given by splicing.

Enter: 1-> 2-> 4, 1-> 3-> 4
output: 1-> 1-> 2-> 3- > 4-> 4

JS实现的合并两个有序链表算法示例 Scheme that can be run directly:

Function Node Element) {this.Element = Element; // The element of the current node this.next = null; // Next Node Link} Function List () {this.Head = New Node (“Head”); // Head Node THIS .find = find; // Find Node this.insert = INSERT; // Insert Node this.remove = remove; // Delete Node this.display = Display; // Show Link Picture THIS.FINDPREVIOUS = FINDPREVIOS; / / Find Previous Node} // The following function is the method of operation: Find the name // of the List class constructor to find a given node function find (item) {var currnode = this.Head; while (currnode.ement! = Item) {currnode = Currnode.next;} Return Currnode;} // Insert a node Function Insert (NEWELEMENT, ITEM) {VAR NEWnode = new node (newElement); var current = this.find (item); if (current == null) Return Console.log (“can’t Find the item”; newnode.next = current.next; current. Next = newNode;} // Delete Node Function Remove (item) {var prevnode = this.findprevious (item); if (prevnode.next! = null) prevnode.next = prevnode.next.next;} // From the list When you delete the node, let’s first find the previous node to be deleted. After finding it, we modify its NEXT attribute so that it is not pointed out to the node to be deleted, but to delete the next node of the node. So, we have to define a FindPRevious method traversal table, check if the next node of each node stores the data to be deleted. If found, return to this node, you can modify its next properties. / / Look for the previous node with delete nodes FUNCTION FINDPREVIOUS (Item) {var currnode = this.head; while (currnode.next! = Null && currnode.next.Element! = Item) {currnode = currnode.next;} return Currnode;} // Show Links DISPLAY () {var current = this.head; while (current.next! = null) {Console.logUrrent.next.ext; current = current.next;}} / ** * @Param} / ** * @Param {node} l2 * @Return {node} * / var merGetwolists = function (L1, L2) { // Mimic the data structure of the linked list var mergedhead = {Element: -1, next: null}, cur = mergehead; while (l1 && l2) {if (l1.element
   <= l2.element){
      cur.next = l1;
      l1 = l1.next;
    }
    else {
      cur.next = l2;
      l2 = l2.next;
    }
    cur = cur.next;
  }
  cur.next = l1 || l2
  return mergedHead.next;
};
let list1 = new List();
list1.insert(1,'head');
list1.insert(2,1);
list1.insert(4,2);
console.log(list1.display());
let list2 = new List();
list2.insert(1,'head');
list2.insert(3,1);
list2.insert(4,3);
console.log(list2.display());
console.log(mergeTwoLists(list1.head,list2.head))
  Interested friends can use 
online HTML / CSS / JavaScript code run tool : http: //tools.jb51.net/code/htmljsrun Test the code to view the running effect.

More about JavaScript related content, readers, interested, can view this topic: “JavaScript Mathematical Computing Summary”, “JavaScript Data Structure and Algorithm Skills Summary”, “JavaScript Action Skills Summary”, ” JavaScript Sort Algorithm Summary

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

Please log in to comment