Combine Two ordered linked lists with another linked list, and ensure the order.

Source: Internet
Author: User

Combine Two ordered linked lists with another linked list, and ensure the order.

Direct Recursion

Code:

# Include <iostream> # include <stack> using namespace std; typedef struct listNode {int key; structlistNode * pNext;} * pNode, Node; void createNode (pNode & pHead) {bool isFirst = true; int temp; scanf ("% d", & temp); pNode p = pHead, q; while (temp! = 0) {q = (pNode) malloc (sizeof (Node); q-> key = temp; q-> pNext = NULL; if (isFirst) {p = pHead = q; isFirst = false;} else {p-> pNext = q; p = q;} scanf ("% d", & temp );} cout <"the original linked list is:" <endl; p = pHead; while (p) {cout <p-> key <""; p = p-> pNext ;}} void print (const pNode merge) {pNode p = merge; while (p) {cout <p-> key <""; p = p-> pNext ;}} pNode merge (const pNode pHead1, const pNode pHead2) {if (pHead1 = NULL) return pHead2; else if (pHead2 = NULL) return pHead1; pNode pMerge = NULL; if (pHead1-> key> pHead2-> key) {pMerge = pHead2; pMerge-> pNext = merge (pHead1, pHead2-> pNext);} else {pMerge = pHead1; pMerge-> pNext = merge (pHead1-> pNext, pHead2);} return pMerge;} void merge (const pNode pHead) {if (pHead = NULL) return; printListReverse (pHead-> pNext); cout <pHead-> key;} int main () {pNode pHead1 = NULL; pNode pHead2 = NULL; createNode (pHead1); createNode (pHead2); pNode merge1 = merge (pHead1, pHead2); print (merge1); return 0 ;}

Running result:



C language combines two ordered linked lists into an ordered linked list (ascending)

Set the Node Structure of the linked List to Node (int data, Node * next), typedef Node List, and the linked List contains all table header nodes. The idea is to regard the elements in list1 as set 1, the elements in list2 as set 2, and the list1 header node (that is, the list1 node) the separation from set 1 is regarded as the header node of the target set. The target set starts with an empty set and always points to the end of the set using the last pointer, then, each time we compare the first element of each element from set 1 and set 2, the smaller one disconnects from the corresponding set and inserts it to the end of the target set list1, that is, the end of the last, and the newly inserted element is used as the new last of the target set list1 until set 1 is empty or set 2 is empty, finally, link the remaining elements in the unempty set to the end of last.

Void Merge (List * list1, List * list2 ){
/* Merge the ordered single-chain table list2 into the ordered single-chain table list1, and make list1 still orderly */
Node * p, * q, * r, * last;
P = list1-> next;/* p always points to the first element of Set 1 */
Q = list2-> next;/* q always points to the first element of Set 2 */
Last = list1;/* regards list1 as the target set, disconnects from set 1, and always points last to the end of the table */
Last-> next = NULL;
While (p & q) {/* When neither set 1 nor set 2 is null */
If (p-> data <q-> data) {/* compare the first element of the two sets, whichever is smaller */
R = p;
P = p-> next;
}
Else {
R = q;
Q = q-> next;
}
/* Detach the minor from the corresponding set and insert it to the end of the target set list1 to make it a new last */
R-> next = last-> next;
Last-> next = r;
Last = r;
}
/* Link the remaining elements in the non-empty set to the end of the last of the target set list1 */
If (p) last-> next = p;
Else last-> next = q;
List2-> next = NULL;/* The current list2 is an empty set. The NULL value is assigned to avoid the next pointer suspension of list2 */
}

How can we combine two ordered linked lists into an ordered linked list?

# Include <stdio. h> # include <stdlib. h> # define N 20 typedef struct LNode {int data; struct LNode * next;} LNode, * LinkList; LinkList CreatList (LinkList & L, int n ); int GetElemList (LinkList & L, int I); LinkList sortList (LinkList & L, int n); void MergeList (LinkList & La, LinkList & Lb, LinkList & Lc ); linkList CreatList (LinkList & L, int n) {int I; LinkList p; L = (LinkList) malloc (sizeof (LNode); L-> next = NULL; for (I = n; I> 0; I --) {p = (LinkList) malloc (sizeof (LNode); p-> data = rand () % 100; p-> next = L-> next; l-> next = p;} return (L);} int GetElemList (LinkList & L, int I) {int j, e; LinkList p; p = L-> next; j = 1; while (p & j <I) {p = p-> next; ++ j;} if (! P | j> I) return (0); e = p-> data; return (e);} LinkList sortList (LinkList & L, int n) {int I, j, t; LinkList p, q; for (I = 0; I <n-1; I ++) {p = L-> next; q = L-> next; for (j = 0; j <n-1-i; j ++) {if (p-> data> q-> data) {t = p-> data; p-> data = q-> data; q-> data = t;} p = p-> next; q = q-> next ;}}for (I = 1; I <= n; I ++) printf ("% 3d", GetElemList (L, I )); printf ("\ n"); return (L);} void MergeList (LinkList & La, LinkList & Lb, LinkList & Lc) {int I; LinkList pa, pb, pc; pa = La-> next; pb = Lb-> next; Lc = pc = La; while (pa & pb) {if (pa-> data <= pb-> data) {pc-> next = pa; pc = pa; pa = pa-> next ;} else {pc-> next = pb; pc = pb; pb = pb-> next ;}} pc-> next = pa? Pa: pb; free (Lb... full text>

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.