# Merging two sequential lists of C + + algorithms

Source: Internet
Author: User

Title: Merging two lists that have been sorted

Method 1:

List of two

As we merge two arrays, the head node of the list 1 is compared with the head nodes of the linked list 2, if the value of the linked List 1 head node is greater than the value of the linked list 2 header point,
Then the first node of the list 2 is the head node of the consolidated list, then the head nodes of the list 1 continue and the second nodes of the linked List 2 (the head nodes of the remaining list 2)
For comparison, but after a linked list has been traversed, if another linked list has not been traversed, because the linked list is already sorted, so let the merge list
The tail node points to the head nodes that have not traversed the linked list.

As an example:

After traversing the list 3:1,2,3,4,8,10, the list 2 has been traversed, while the loop exits, but the remaining list 1 has 23,34
At this point let the tail node of the list 3 10 link The remaining list of the head node 23 will be able to

`<span style= "color: #000000;" >//merge linked list. cpp: The entry point that defines the console application. #include "stdafx.h" #include <iostream>using namespace std;struct listnode{int m_data; listnode* M_pnext;  ListNode (int value,listnode* next = NULL): m_data (value), M_pnext (next) {}};/* Two linked lists such as linked list 1:1->3->5->7->9 List 2: 2->4->6->8->10 with us to merge two arrays, the head node of the chain Table 1 and the head nodes of the linked list 2, if the value of the linked List 1 head node is greater than the value of the linked List 2 head contact, then the head node of the list 2 is the head node of the Consolidated list, Then the head node of List 1 continues to be compared with the second node of List 2 (the head node of the remaining list 2). But after a linked list has been traversed, if another linked list has not been traversed, because the linked list is already sorted, so that the tail node of the merged list points to the head nodes of the list that have not been traversed, you can give an example: List 1: 1,3,5,23,34; chain list 2:2,4,6,8,10; When the list is 3:1,2,3,4,8,10, the chain table 2 has been traversed, while the loop exits, but the remaining list 1 also has 23,34 at this point, the tail node of the linked list 3 10 links to the remaining list of the head node 23 can be With */listnode* MergeList2 (listnode* head1,listnode* head2) {if (Head1 = = NULL) {return head2;} else if (head2 = = NULL) {return head1;} listnode* Mergehead = null;if (Head1->m_data < head2->m_data) {Mergehead = Head1;head1 = Head1->m_pNext;} Else{mergehead = Head2;head2 = Head2->m_pnext;} listnode* Tmpnode = Mergehead;while (head1&&head2) {if (head1->M_data < Head2->m_data) {Mergehead->m_pnext = Head1;head1 = Head1->m_pnext;} Else{mergehead->m_pnext = Head2;head2 = Head2->m_pnext;} Mergehead = Mergehead->m_pnext;} if (head1) {mergehead->m_pnext = Head1;} if (head2) {mergehead->m_pnext = head2;} return tmpnode;} int _tmain (int argc, _tchar* argv[]) {listnode* pHead1 = new ListNode (1); listnode* pcur = phead1;for (int i = 3; i <; i+=2) {listnode* Tmpnode = new ListNode (i);p cur->m_pnext = tmpnode;pc ur = tmpnode;} listnode* pHead2 = new ListNode (2);p cur = phead2;for (int j = 4; J < j+=2) {listnode* Tmpnode = new ListNode (j);p cur ->m_pnext = Tmpnode;pcur = Tmpnode;} listnode* head = MergeList2 (phead1,phead2), while (head) {cout<Method 2:/*We analyzed the two linked list process, first from the merging of two linked list of head nodes, the value of the head node of the linked list 1 is less than the value of the header of the linked List 2, so the node of the chain table 1 headeris to merge the head node of the linked list, continue to merge the rest of the list, the remaining nodes in the two linked list is still sorted, so the steps to merge two lists are the same, we still compare two head nodesValue, the value of the head node of the linked list 2 is less than the value of the head node of the linked list 1, so the head node of the linked List 2 is the head junction of the consolidated list, and we put the node and the tail of the linked list when the linked list was previously mergedLink upAccording to the above analysis, the steps of each merger are the same, so we think of recursion.*/Merge linked list. cpp: The entry point that defines the console application. #include "stdafx.h" #include <iostream>using namespace std;struct listnode{int m_data; listnode* M_pnext; ListNode (int value,listnode* next = NULL): m_data (value), M_pnext (next) {}};/* We analyze the process of two linked lists, first starting from merging the head nodes of the two linked lists, The value of the head node of the linked list 1 is less than the value of the header nodes of the linked list 2, so the head node of the list 1 is the head of the merged list, continue to merge the rest of the list, the remaining nodes in the two linked lists are still sorted, so the steps to merge two lists are the same, we still compare the values of two head nodes, At this point, the value of the head node of the linked list 2 is less than the value of the head node of the linked list 1. Therefore, the head node of the list 2 is the head node of the consolidated remaining list, and we link this node with the tail nodes of the linked list when the linked list was previously merged, according to the above analysis: the steps of each merge are the same, and we think of recursion. */listnode* mergelist (listnode* phead1,listnode* pHead2) {if (PHead1 = = NULL) {return pHead2;} else if (pHead2 = = NULL) {return pHead1;} listnode* Pmergehead = null;if (Phead1->m_data < phead2->m_data) {Pmergehead = Phead1;pmergehead->m_pnext = Mergelist (phead1->m_pnext,phead2);} Else{pmergehead = Phead2;pmergehead->m_pnext = Mergelist (Phead1,phead2->m_pnext);} return pmergehead;} int _tmain (int argc, _tchar* argv[]) {listnode* pHead1 = new ListNode (1); listnode* pcur = phead1;for (int i = 3; i <; i+=2) {listnode* Tmpnode= new ListNode (i);p cur->m_pnext = Tmpnode;pcur = Tmpnode;} listnode* pHead2 = new ListNode (2);p cur = phead2;for (int j = 4; J < j+=2) {listnode* Tmpnode = new ListNode (j);p cur ->m_pnext = Tmpnode;pcur = Tmpnode;} listnode* head = MergeList2 (phead1,phead2), while (head) {cout<Looking at this topic, the last merged array can also be used recursively to enclose the code:<span style= "color: #000000;" >//MergeArray.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;//recursive method void MergeArray2 (int a[],int acount,int b[], int blen) {int len = acount+blen-1;acount--;blen--;if (acount < 0) {while (blen>=0) {a[len--] = b[blen--];} return;} if (A[acount] > B[blen]) {A[len] = A[acount]; MergeArray2 (A,acount,b,++blen);} Else{a[len] = B[blen]; MergeArray2 (A,++acount,b,blen);}} void Mergearray (int a[], int acount, int b[], int blen)//acount to the actual (narrow) length of a array, Blen to B array actual length {int len = acount + blen-1;//Merge number The length of the group is the generalized length of the A array acount--;blen--;while (acount>=0 && blen>=0) {if (A[acount] >= B[blen]) {a[len--] = a[ acount--];} else{a[len--] = b[blen--];}} while (Blen >= 0) {a[len--] = b[blen--];}} int _tmain (int argc, _tchar* argv[]) {int a[] = {2,4,6,8,10,0,0,0,0,0};int b[] = {1,3,5,7,9}; MergeArray2 (a,5,b,5); for (int i = 0; i < sizeof (a)/sizeof (a[0]); i++) {cout<<a[i]<< "";} GetChar (); return 0;} </span>Personal feeling merging arrays with recursion is not good, because it is a bit cumbersome to consider if an array has traversed the other array, and if it is a linked list, a number of linked lists,Then this list is empty, then return to another linked list, that is, the previous merged linked list Automatic link on the other has not been traversed the part of the list!Merging two sequential lists of C + + algorithms`
Related Keywords:
Related Article

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.