JavaScript implements the sorting of linked list insertion and list merging, and javascript merging.

Source: Internet
Author: User

JavaScript implements the sorting of linked list insertion and list merging, and javascript merging.

This article describes in detail how to sort the inserted and merged linked lists in JavaScript. The merged sorting of the linked list is to sort and merge each part.

1. Linked List

1.1 storage representation of the linked list

// The storage of the linked list indicates typedef int ElemType; typedef struct LNode {ElemType data; struct LNode * next;} LNode, * LinkList;

1.2 Basic operations

Create a linked list:

/** Create a linked list. * The parameter num is the length of the linked list. The function returns the header pointer of the linked list. */LinkList CreatLink (int num) {int I, data; // p points to the last node in the current linked list, and q points to the node to be inserted. LinkList head = NULL, p = NULL, q; for (I = 0; I <num; I ++) {scanf ("% d", & data ); q = (LinkList) malloc (sizeof (LNode); q-> data = data; q-> next = NULL; if (I = 0) {head = q ;} else {p-> next = q;} p = q;} return head ;}

Output linked list:

/** Output the linked list node value. */Int PrintLink (LinkList head) {LinkList p; for (p = head; p = p-> next) {printf ("%-3d ", p-> data) ;}return 0 ;}

2. Sort linked list Inserts

Basic Idea: Assuming n-1-1 nodes are ordered, insert the n-th node to the appropriate position of the previous node to make the n nodes orderly.

Implementation Method:

Split the first node in the linked list into a linked list containing one node (head1). The other nodes naturally become another linked list (head2 ).

An ordered linked list of a node;

Split the first node of the linked list head2 and insert it to the proper position of the linked list head1 so that head1 is still ordered. At this time, head1 becomes an ordered linked list containing two nodes;

Split a node from the linked list head2 and insert it into the linked list head1 until the linked list head2 is empty. Finally, the linked list head1 contains all nodes and the nodes are ordered.

Insert the Sorting code:

/** Sort the inserted chain table (from small to large ). * Input: Head pointer of the linked list, * output: Head pointer of the sorted linked list. * Implementation Method: Split the original Linked List into two parts: Linked List 1 still uses the head as the pointer, and the linked list nodes are ordered. Linked List 2 uses head2 as the head pointer, and the linked list nodes are unordered. * Insert the nodes in linked list 2 to Linked List 1 in sequence, and keep linked list 1 in order. * Finally, linked list 1 contains all nodes and is ordered. */LinkList LinkInsertSort (LinkList head) {// current points to the node to be inserted. LinkList head2, current, p, q; if (head = NULL) return head; // The first split. Head2 = head-> next; head-> next = NULL; while (head2) {current = head2; head2 = head2-> next; // find the insert position, the insert position is in the center of node p and q. For (p = NULL, q = head; q & q-> data <= current-> data; p = q, q = q-> next ); if (q = head) {// insert current to the beginning. Head = current;} else {p-> next = current;} current-> next = q;} return head ;}

Complete source code:

/** Sort the inserted chain table, from small to large */# define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # define TOTAL 10 // chain table length // the storage of the chain table indicates typedef int ElemType; typedef struct LNode {ElemType data; struct LNode * next;} LNode, * LinkList; linkList CreatLink (int num); LinkList LinkInsertSort (LinkList head); int PrintLink (LinkList head);/** create a linked list. * The parameter num is the length of the linked list. The function returns the header pointer of the linked list. */LinkList CreatLink (int num) {int I, data; // p points to the last node in the current linked list, and q points to the node to be inserted. LinkList head = NULL, p = NULL, q; for (I = 0; I <num; I ++) {scanf ("% d", & data ); q = (LinkList) malloc (sizeof (LNode); q-> data = data; q-> next = NULL; if (I = 0) {head = q ;} else {p-> next = q;} p = q;} return head;}/** sort the inserted chain table (from small to large ). * Input: Head pointer of the linked list, * output: Head pointer of the sorted linked list. * Implementation Method: Split the original Linked List into two parts: Linked List 1 still uses the head as the pointer, and the linked list nodes are ordered. Linked List 2 uses head2 as the head pointer, and the linked list nodes are unordered. * Insert the nodes in linked list 2 to Linked List 1 in sequence, and keep linked list 1 in order. * Finally, linked list 1 contains all nodes and is ordered. */LinkList LinkInsertSort (LinkList head) {// current points to the node to be inserted. LinkList head2, current, p, q; if (head = NULL) return head; // The first split. Head2 = head-> next; head-> next = NULL; while (head2) {current = head2; head2 = head2-> next; // find the insert position, the insert position is in the center of node p and q. For (p = NULL, q = head; q & q-> data <= current-> data; p = q, q = q-> next ); if (q = head) {// insert current to the beginning. Head = current;} else {p-> next = current;} current-> next = q;} return head;}/** output the linked list node value. */Int PrintLink (LinkList head) {LinkList p; for (p = head; p = p-> next) {printf ("%-3d ", p-> data);} return 0;} int main () {LinkList head; printf ("Enter the Total number to create a linked list: \ n "); head = CreatLink (TOTAL); head = LinkInsertSort (head); printf ("sorted: \ n"); PrintLink (head); putchar ('\ n '); return 0 ;}

3. sort and merge linked lists

Basic Idea: If the linked list is empty or contains a node, the linked list is naturally ordered. Otherwise, the linked list is divided into two parts, and each part is sorted separately, and then the two linked lists are merged together.

Merge Sort code:

/** Sort and merge the linked lists (from small to large ). * Input: Head pointer of the linked list, * output: Head pointer of the sorted linked list. * Recursive Implementation Method: divides the head of a linked list into two parts: Merge and sort, and then merge the two parts. * Recursive end condition: the linked list for Recursive sorting is empty or has only one node. */LinkList LinkMergeSort (LinkList head) {LinkList head1, head2; if (head = NULL | head-> next = NULL) return head; LinkSplit (head, & head1, & head2); head1 = LinkMergeSort (head1); head2 = LinkMergeSort (head2); head = LinkMerge (head1, head2); return head ;}

The linked list splitting function is as follows. The basic idea is to use the slow/fast pointer. For details about the implementation method, see annotations.

/** Chain Table Partitioning function. * The head of the linked list is divided into two parts: head1 and head2. If the length of the linked list is an odd number, the extra nodes belong to the first part. * Implementation Method: first point the pointer slow/fast to the beginning of the chain. * then, when the fast pointer moves forward to the same two nodes, the slow pointer moves forward to one node and * cyclically moves, until the fast Pointer Points to the end of the chain. At the end, slow points to the end of the chain table head1. */Int LinkSplit (LinkList head, LinkList * head1, LinkList * head2) {LinkList slow, fast; if (head = NULL | head-> next = NULL) {* head1 = head; * head2 = NULL; return 0;} slow = head; fast = head-> next; while (fast) {fast = fast-> next; if (fast) {fast = fast-> next; slow = slow-> next;} * head1 = head; * head2 = slow-> next; // note: be sure to leave the chain tail of the chain table head1 empty. Slow-> next = NULL; return 0 ;}

The linked list merging function has two methods: Recursive Implementation and non-Recursive Implementation:

Non-Recursive Implementation:

/** Merge linked lists. * Combine two ordered linked lists to make the total linked lists orderly. * Input: chain table head1 and chain table head2 * output: merged chain table * Implementation Method: insert the nodes in the chain table head2 to the appropriate position in the chain table head1 in sequence so that head1 remains an ordered chain table. */LinkList LinkMerge (LinkList head1, LinkList head2) {LinkList p, q, t; if (! Head1) return head2; if (! Head2) return head1; // initialize the cyclic variable. q points to the current node in the linked list head1, and p is the precursor of q. P = NULL; q = head1; while (head2) {// t is the node to be inserted. T = head2; head2 = head2-> next; // search for the insert position, which is between p and q. For (; q & q-> data <= t-> data; p = q, q = q-> next); if (p = NULL) head1 = t; else p-> next = t; t-> next = q; // Insert the node t between p and q, so that p points to the q precursor again. P = t;} return head1 ;}

Recursive Implementation:

LinkList LinkMerge2(LinkList head1, LinkList head2){  LinkList result;   if (!head1)    return head2;  if (!head2)    return head1;   if (head1->data <= head2->data)  {    result = head1;    result->next = LinkMerge(head1->next, head2);  }  else  {    result = head2;    result->next = LinkMerge(head1, head2->next);  }  return result;}

Complete source code:

/** Sort linked lists in ascending order. */# Define _ CRT_SECURE_NO_WARNINGS # include <stdio. h> # include <stdlib. h> # define TOTAL 10 // chain table length // the storage of the chain table indicates typedef int ElemType; typedef struct LNode {ElemType data; struct LNode * next;} LNode, * LinkList; linkList CreatLink (int num); LinkList LinkMergeSort (LinkList head); LinkList LinkMerge (LinkList head1, LinkList head2); LinkList LinkMerge2 (LinkList head1, LinkList head2); int LinkSplit (LinkList head, linkLi St * head1, LinkList * head2); int PrintLink (LinkList head);/** create a linked list. * The parameter num is the length of the linked list. The function returns the header pointer of the linked list. */LinkList CreatLink (int num) {int I, data; // p points to the last node in the current linked list, and q points to the node to be inserted. LinkList head = NULL, p = NULL, q; for (I = 0; I <num; I ++) {scanf ("% d", & data ); q = (LinkList) malloc (sizeof (LNode); q-> data = data; q-> next = NULL; if (I = 0) {head = q ;} else {p-> next = q;} p = q;} return head;}/** output the linked list node value. */Int PrintLink (LinkList head) {LinkList p; for (p = head; p = p-> next) {printf ("%-3d ", p-> data);} return 0;} int main () {LinkList head; printf ("Enter the Total number to create a linked list: \ n "); head = CreatLink (TOTAL); head = LinkMergeSort (head); printf ("sorted: \ n"); PrintLink (head); putchar ('\ n '); return 0;}/** sort and merge the Linked List (from small to large ). * Input: Head pointer of the linked list, * output: Head pointer of the sorted linked list. * Recursive Implementation Method: divides the head of a linked list into two parts: Merge and sort, and then merge the two parts. * Recursive end condition: the linked list for Recursive sorting is empty or has only one node. */LinkList LinkMergeSort (LinkList head) {LinkList head1, head2; if (head = NULL | head-> next = NULL) return head; LinkSplit (head, & head1, & head2); head1 = LinkMergeSort (head1); head2 = LinkMergeSort (head2); head = LinkMerge (head1, head2); // non-Recursive Implementation // head = LinkMerge2 (head1, head2); // implement return head recursively;}/** merge linked lists. * Combine two ordered linked lists to make the total linked lists orderly. * Input: chain table head1 and chain table head2 * output: merged chain table * Implementation Method: insert the nodes in the chain table head2 to the appropriate position in the chain table head1 in sequence so that head1 remains an ordered chain table. */LinkList LinkMerge (LinkList head1, LinkList head2) {LinkList p, q, t; if (! Head1) return head2; if (! Head2) return head1; // initialize the cyclic variable. q points to the current node in the linked list head1, and p is the precursor of q. P = NULL; q = head1; while (head2) {// t is the node to be inserted. T = head2; head2 = head2-> next; // search for the insert position, which is between p and q. For (; q & q-> data <= t-> data; p = q, q = q-> next); if (p = NULL) head1 = t; else p-> next = t; t-> next = q; // Insert the node t between p and q, so that p points to the q precursor again. P = t;} return head1;} LinkList LinkMerge2 (LinkList head1, LinkList head2) {LinkList result; if (! Head1) return head2; if (! Head2) return head1; if (head1-> data <= head2-> data) {result = head1; result-> next = LinkMerge (head1-> next, head2 );} else {result = head2; result-> next = LinkMerge (head1, head2-> next);} return result ;}/ ** linked list partitioning function. * The head of the linked list is divided into two parts: head1 and head2. If the length of the linked list is an odd number, the extra nodes belong to the first part. * Implementation Method: first point the pointer slow/fast to the beginning of the chain. * then, when the fast pointer moves forward to the same two nodes, the slow pointer moves forward to one node and * cyclically moves, until the fast Pointer Points to the end of the chain. At the end, slow points to the end of the chain table head1. */Int LinkSplit (LinkList head, LinkList * head1, LinkList * head2) {LinkList slow, fast; if (head = NULL | head-> next = NULL) {* head1 = head; * head2 = NULL; return 0;} slow = head; fast = head-> next; while (fast) {fast = fast-> next; if (fast) {fast = fast-> next; slow = slow-> next;} * head1 = head; * head2 = slow-> next; // note: be sure to leave the chain tail of the chain table head1 empty. Slow-> next = NULL; return 0 ;}

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.