Insert/sort/merge/sort
1. the storage of the linked list 1.1 table means that the storage of the linked list indicates typedef int ElemType; typedef struct LNode {ElemType data; struct LNode * next;} LNode, * LinkList; 1.2 Basic operation/** 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 linked list node value. */Int PrintLink (LinkList head) {LinkList p; for (p = head; p = p-> next) {printf ("%-3d ", p-> data);} return 0;} 2. the basic idea of inserting and sorting a linked list: Assuming n-1-1 nodes are ordered, insert the nth node into the appropriate position of the first node to make the n nodes orderly. Implementation Method: remove the first node in the linked list to become a linked list containing a node (head1). The other nodes naturally become another linked list (head2 ), at this time, head1 is an ordered linked list containing one node. The first node in the linked list head2 is split and inserted 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. One node is split from the linked list head2 and inserted 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 Sorting Code:/** sort by insert in the Linked List (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: * sorting of linked list insertion, 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. the basic idea of chain table merging and sorting: if the chain table is empty or contains a node, the chain table 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 and Sort code:/** merge and sort 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); return head;} the chain Table Partitioning function is as follows, the basic idea is to use the slow/fast pointer. For specific implementation methods, 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:/** linked list merging. * 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 the linked list 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, lin KList * 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 ;}