十年網(wǎng)站開發(fā)經(jīng)驗 + 多家企業(yè)客戶 + 靠譜的建站團(tuán)隊
量身定制 + 運(yùn)營維護(hù)+專業(yè)推廣+無憂售后,網(wǎng)站問題一站解決
本篇內(nèi)容介紹了“C++怎么實現(xiàn)3個鏈表排序整合到一起”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司是專業(yè)的瀍河網(wǎng)站建設(shè)公司,瀍河接單;提供成都網(wǎng)站設(shè)計、做網(wǎng)站,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行瀍河網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊,希望更多企業(yè)前來合作!
當(dāng)兩個 遞增鏈表時 可以采用 歸并排序 或者 二叉樹建立時候的遞歸。
#include#include using namespace std; typedef struct node { int data; struct node* next; }NODE; NODE* CreateList(int a[], int n) { NODE* p; NODE* q; NODE* head; p = new NODE; p->data = a[0]; head = p; for(int i=1;i data = a[i]; p->next = q; p = q; } p->next = NULL; return head; } //整體思路跟 排序算法中的 歸并排序類似 NODE* MerageList(NODE* head_1, NODE* head_2) { NODE* head_3 = NULL; if(head_1 == NULL) { head_3 = head_2; } else if(head_2 == NULL) { head_3 = head_1; } else { NODE* top1 =head_1; NODE* top2 =head_2; NODE* mid = NULL; if(top1->data < top2->data) { head_3 = top1; mid =head_3 ; top1 = top1->next; } else { head_3 = top2; mid = head_3 ; top2 = top2->next; } // 找到合并后頭節(jié)點 while(top1 && top2) { if(top1->data < top2->data) { mid->next = top1; mid = top1; top1 = top1->next; } else { mid->next = top2; mid = top2; top2 = top2->next; } } if(top1) { mid->next = top1; } else if(top2) { mid->next = top2; } } return head_3; } //我們可以采用二叉樹插入時的 遞歸法 NODE* MerageByDG(NODE* head_1, NODE* head_2) { if(NULL == head_1) { return head_2; } else if(NULL == head_2) { return head_1; } NODE* head_3; if(head_1->data < head_2->data) { head_3 = head_1; head_3->next = MerageByDG(head_1->next, head_2); } else { head_3 = head_2; head_3->next = MerageByDG(head_1, head_2->next); } return head_3; } int main() { int a[]={1,3,5,7}; int c[]={2,4,6,8,10}; NODE* list1 = CreateList(a,4); NODE* list3 = CreateList(c,5); list1=MerageByDG(list1, list3); for(NODE* temp =list1;temp; temp = temp->next) { cout<< temp->data<<" "; } cout<
普通數(shù)據(jù)時可用如下方法 用冒泡排序法
#include#include using namespace std; typedef struct node { int data; struct node* next; }NODE; NODE* CreateList(int a[], int n) { NODE* p; NODE* q; NODE* head; p = new NODE; p->data = a[0]; head =p; for(int i=1;i data = a[i]; p->next = q; p = q; } p->next = NULL; return head; } void BubbleSort(NODE* list) { for(NODE* temp1= list;temp1; temp1=temp1->next) { for(NODE* temp2 =temp1->next; temp2; temp2=temp2->next) { if(temp1->data >temp2->data) { int temp = temp1->data; temp1->data = temp2->data; temp2->data = temp; } } } } NODE* Merge (NODE* list1, NODE* list2) { NODE* temp2=list2; NODE* temp1=list1; NODE* front=NULL; for(;temp2; temp2 = temp2->next) { for(; temp1; front = temp1,temp1=temp1->next) { if( ((temp2->data) < (temp1->data)) && (front==NULL)) { NODE* temp = new NODE; temp->data = temp2->data; temp->next = list1; list1 = temp; break; } else if( (front != NULL) && (front->data < temp2->data) && (temp1->data >= temp2->data) ) { NODE* temp = new NODE; temp->data = temp2->data; temp->next = temp1; front->next = temp; break; } else if ( ( (temp2->data)>(temp1->data) ) && (NULL==temp1->next) ) { NODE* temp = new NODE; temp->data = temp2->data; temp1->next = temp; temp->next =NULL; break; } } temp1 = list1; front = NULL; } return list1; } int main() { int a[]={5,6,6,7}; int b[]={8,6,4,1}; int c[]={3,5,6,7,8}; NODE* list1 = CreateList(a,4); NODE* list2 = CreateList(b,4); NODE* list3 = CreateList(c,5); list1=Merge(list1, list2); list1=Merge(list1,list3); for(NODE* temp =list1;temp; temp = temp->next) { cout<< temp->data<<" "; } cout< next) { cout<< temp->data<<" "; } cout< “C++怎么實現(xiàn)3個鏈表排序整合到一起”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
本文標(biāo)題:C++怎么實現(xiàn)3個鏈表排序整合到一起
URL鏈接:http://m.jiaotiyi.com/article/pidosh.html