Skip to content

Commit 7a1e740

Browse files
committed
general ListMerge
1 parent db5b685 commit 7a1e740

File tree

2 files changed

+88
-14
lines changed

2 files changed

+88
-14
lines changed

BinarySortTree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int main(){
9999
InsertBST(&bintree , n);
100100

101101
cout << "中序遍历:" <<endl;
102-
InOrderTraversal(bintree);
102+
//InOrderTraversal(bintree);
103103

104104
cin >>n;
105105
cout <<"输入要删除的数:" <<endl;

ListMerge.cpp

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include <stdlib.h>
33
using namespace std;
44

5+
/**
6+
* 增序链表合并
7+
*/
8+
59
typedef struct List{
610
int num;
711
List *next;
@@ -16,6 +20,17 @@ void print(List *list){
1620
cout <<""<<endl;
1721
}
1822

23+
void print2(List *PC){
24+
while(PC->next){
25+
cout <<PC->next->num<< " ";
26+
PC = PC->next;
27+
}
28+
cout << "" <<endl;
29+
}
30+
31+
/**
32+
* 递归合并
33+
*/
1934
List *MergeList(List *list , List *list2){
2035
if(list == NULL)
2136
return list2;
@@ -34,32 +49,91 @@ List *MergeList(List *list , List *list2){
3449
return temp;
3550
}
3651

37-
int main(){
38-
List *l1 = new List;
39-
l1->next = NULL;
40-
l1->num = 0;
41-
List *l2 = new List;
42-
l2->next = NULL;
43-
l2->num = 1;
52+
/**
53+
* 普通合并
54+
*/
55+
List *MergeList2(List *PA , List *PB , List *PC){
56+
List *pa = PA->next;
57+
List *pb = PB->next;
58+
PC = PA;
59+
List *pc = PC;
60+
while(pa&&pb){
61+
if(pa->num >= pb->num){
62+
pc->next = pb;
63+
pc = pc->next;
64+
pb = pb->next;
65+
}else{
66+
pc->next = pa;
67+
pc= pc->next;
68+
pa = pa->next;
69+
}
70+
}
71+
72+
pc->next = pa?pa:pb;
73+
return PC;
74+
}
75+
76+
/**
77+
* 初始化链表
78+
*/
79+
void InitList(List **l1 , List **l2){
80+
(*l1) = new List;
81+
(*l1)->next = NULL;
82+
(*l1)->num = 0;
83+
(*l2) = new List;
84+
(*l2)->next = NULL;
85+
(*l2)->num = 1;
4486
for(int i = 10 ;i >= 2 ;i -= 2){
4587
List *t = new List;
46-
t->next = l1->next;
47-
l1->next = t;
88+
t->next = (*l1)->next;
89+
(*l1)->next = t;
4890
t->num = i;
4991
}
5092

5193
for(int i = 11 ;i >= 3 ;i -= 2){
5294
List *t = new List;
53-
t->next = l2->next;
54-
l2->next = t;
95+
t->next = (*l2)->next;
96+
(*l2)->next = t;
5597
t->num = i;
5698
}
99+
}
57100

101+
/**
102+
* 初始化带头结点链表
103+
*/
104+
void InitListWithHead(List **LA , List **LB){
105+
*LA = new List;
106+
*LB = new List;
107+
(*LA)->next = NULL;
108+
(*LB)->next = NULL;
109+
for(int i = 10;i >= 0 ;i -=2){
110+
List *t = new List;
111+
t->num = i;
112+
t->next = (*LA)->next;
113+
(*LA)->next = t;
114+
}
115+
for(int i = 11 ;i >= 1 ;i -=2){
116+
List *t = new List;
117+
t->num = i;
118+
t->next = (*LB)->next;
119+
(*LB)->next = t;
120+
}
121+
}
122+
123+
int main(){
124+
cout << "递归合并" <<endl;
125+
List *l1,*l2,*l3;
126+
InitList(&l1 , &l2);
58127
print(l1);
59128
print(l2);
60-
61-
cout << "ÅÅÐòºó" <<endl;
62129
print(MergeList(l1 ,l2));
63130

131+
cout << "普通合并" <<endl;
132+
List *LA , *LB, *LC;
133+
InitListWithHead(&LA , &LB);
134+
print2(LA);
135+
print2(LB);
136+
print2(MergeList2(LA,LB,LC));
137+
64138
return 0;
65139
}

0 commit comments

Comments
 (0)