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 = 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 =;} 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”; =; current. Next = newNode;} // Delete Node Function Remove (item) {var prevnode = this.findprevious (item); if (! = null) =;} // 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 (! = Null &&! = Item) {currnode =;} return Currnode;} // Show Links DISPLAY () {var current = this.head; while (! = null) {; current =;}} / ** * @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){ = l1;
      l1 =;
    else { = l2;
      l2 =;
    cur =;
  } = l1 || l2
let list1 = new List();
let list2 = new List();
  Interested friends can use 
online HTML / CSS / JavaScript code run tool : http: // 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
Just support it if you like
comment Grab the couch

Please log in to comment