1. 程式人生 > >多個有序鏈表的合並

多個有序鏈表的合並

全局 函數 cout 返回值 簡化 復用 有序鏈表合並 ack span

1, 先將問題簡化,合並兩個有序鏈表

首先分析合並兩個鏈表的過程。我們的分析從合並兩個鏈表的頭結點開始。鏈表1的頭結點的值小於鏈表2的頭結點的值,因此鏈表1的頭結點將是合並後鏈表的頭結點。如下圖所示。

技術分享

參考:http://www.cnblogs.com/jason2013/articles/4341153.html

使用遞歸方法,一步步生成頭結點,代碼如下

遞歸的要訣是子問題要和父問題完全一樣,只是規模變小(每次調用,更小的參數值),

 1 List merge(List head1, List head2){
 2     List mergeHead = NULL;
 3     if (head1 == NULL) {
4 return head2; 5 } 6 if (head2 == NULL){ 7 return head1; 8 } 9 10 if (head1->item < head2->item){ 11 mergeHead = head1; 12 mergeHead->next = merge(head1->next, head2); 13 }else{ 14 mergeHead = head2; 15 mergeHead->next = merge(head1, head2->next);
16 } 17 return mergeHead; 18 }

2, 當有多個鏈表時,考慮分治法每兩個鏈表進行合並.

確定父子函數原型,要想使用遞歸,子問題和父問題必須完全一樣(即返回值,參數類型完全一致)

父問題:多個鏈表

子問題:n/2,...,2個鏈表,1個鏈表

遞歸函數原型List mergeList(int l, int r)

父子問題都是返回一個合並後鏈表,

使用l,r 兩個變量控制問題規模,指定鏈表個數(快速排序,歸並排序都喜歡用這樣的兩個參數)

將多個鏈表存放在全局變量vector<List> lists中,簡化遞歸函數.

第9行代碼復用前面提到的兩個有序鏈表合並

 1 List mergeList(int l, int r){
 2     List u, v;
 3     int m = (l + r) / 2;
 4     if (l == r) {
 5         return lists[l];
 6     }
 7     u = mergeList(l, m);
 8     v = mergeList(m + 1, r);
 9     return merge(u, v);
10 }

3, main 函數

 1 int main(void)
 2 {
 3     int size = 8;
 4     int num = 5;
 5     ListFactory(size, num);
 6     for (int i = 0; i < size; i++){
 7         print(lists[i]);
 8     }
 9     cout << endl;
10     link t = mergeList(0, size-1);
11     print(t);
12     return 0;
13 }

效果

1->9->17->25->33->
2->10->18->26->34->
3->11->19->27->35->
4->12->20->28->36->
5->13->21->29->37->
6->14->22->30->38->
7->15->23->31->39->
8->16->24->32->40->

1->2->3->4->5->6->7->8->9->10->11->12->13->14->15->16->17->18->19->20->21->22->23->24->25->26->27->28->29->30->31->32->33->34->35->36->37->38->39->40->

完整程序

技術分享
 1 #include<iostream>
 2 #include<string>
 3 #include<vector>
 4 using std::cin;
 5 using std::cout;
 6 using std::endl;
 7 using std::string;
 8 using std::vector;
 9 typedef struct node* link;
10 struct node{
11     int item;
12     link next;
13 };
14 typedef link List;
15 vector<List> lists;
16 void print(List list){
17     while (list != NULL){
18         cout << list->item<< "->";
19         list = list->next;
20     }
21     cout << endl;
22 }
23 
24 vector<List> ListFactory(int num, int size){
25     for (int k = 1; k <= num; k++){
26         link t = (link)malloc(sizeof *t);
27         t->item = k;
28         t->next = t;
29         link x = t;
30         for (int m = k + num; m <= num*size; m = m+num){
31             x = (x->next = (link)malloc(sizeof *x));
32             x->item = m;
33             x->next = t;
34         }
35         x->next = NULL;
36         lists.push_back(t);
37     }
38     return lists;
39 }
40 
41 List merge(List head1, List head2){
42     List mergeHead = NULL;
43     if (head1 == NULL) {
44         return head2;
45     }
46     if (head2 == NULL){
47         return head1;
48     }
49 
50     if (head1->item < head2->item){
51         mergeHead = head1;
52         mergeHead->next = merge(head1->next, head2);
53     }else{
54         mergeHead = head2;
55         mergeHead->next = merge(head1, head2->next);
56     }
57     return mergeHead;
58 }
59 
60 List mergeList(int l, int r){
61     List u, v;
62     int m = (l + r) / 2;
63     if (l == r) {
64         return lists[l];
65     }
66     u = mergeList(l, m);
67     v = mergeList(m + 1, r);
68     return merge(u, v);
69 }
70 
71 int main(void)
72 {
73     int size = 8;
74     int num = 5;
75     ListFactory(size, num);
76     for (int i = 0; i < size; i++){
77         print(lists[i]);
78     }
79     cout << endl;
80     link t = mergeList(0, size-1);
81     print(t);
82     return 0;
83 }
View Code

多個有序鏈表的合並