Merge ordered Single-Chain tables. We know that the two linked lists head1 and head2 are ordered. combine them into a linked list and they are still ordered. Use non-recursive and recursive methods respectively.
Resolution:
First, we will introduce non-recursive methods. Because the two linked lists head1 and head2 are ordered, you only need to insert the elements of the shorter linked list into the longer-chain table in sequence.
Program code:
// Insert node * insert_node (node * head, node * Item) {node * P = head; node * q = NULL; // point to the node before P while (p-> data <item-> Data & P! = NULL) {q = P; P = p-> next;} If (P = head) // insert it before the original header node {item-> next = P; return head;} // insert to the Q-> next = item; item-> next = P; return head;} int length (node * head) {int Len = 0; node * P = head; while (null! = P) {Len ++; P = p-> next;} return Len;} // combine the two sorted linked lists node * my_merge (node * head1, node * head2) {node * head = NULL; // points to the merged linked list node * P = NULL; node * nextp = NULL; // points to P after if (null = head1) // if one linked list is empty, another linked list {return head2;} If (null = head2) {return head1;} is directly returned ;} // neither of the two linked lists is empty if (length (head1)> length (head2) // select a shorter linked list {// This operation requires fewer head = head1; P = head2;} else {head = head2; P = head1;} while (null! = P) {nextp = p-> next; // save P's next node head = insert_node (Head, P); // insert P to the target linked list p = nextp; // point to the next node to be inserted} return head ;}
Here, the insert_node () function is the insertion of ordered nodes. Note the difference from the function in the previous article. Here, the input parameter is of the node * type. Then, all the nodes in the short linked list are inserted into the long chain table cyclically in the my_merge () function.
Next we will introduce recursive methods. Assume there are two linked lists.
Linked List-> 3-> 5
Linked List-> 4-> 6
The procedure for recursion is as follows:
(1) compare the data of 1st nodes in linked list 1 and linked list 2. Because 1 <2, the head node of the result linked list points to 1st nodes in linked list 1, that is, the node where data 1 is located.
(2) Call the (1) method for the remaining linked list 1 (3-> 5) and linked list 2 and compare the 2nd nodes in the result linked list, 2 is compared with 3, and the merged linked list node is 1-> 2. The program code is as follows:
// Recursive method combines two ordered linked lists: node * mergerecursive (node * head1, node * head2) {node * head = NULL; If (null = head1) {return head2 ;} if (null = head2) {return head1;} If (head1-> data
The following is the test procedure:
Void print (node * head) {node * P = NULL; int Index = 0; If (null = head-> next) {printf ("link is empty! \ N "); return;} p = head-> next; while (null! = P) {printf ("the % DTH node % d \ n", ++ index, p-> data); P = p-> next ;}} int main () {node * head1 = create (); node * head2 = create (); // node * head = my_merge (head1, head2); head1 = head1-> next; // first, one of the two linked lists is required to give headnode * head = mergerecursive (head1, head2); print (head); Return 0 ;}