File tree Expand file tree Collapse file tree 2 files changed +88
-14
lines changed Expand file tree Collapse file tree 2 files changed +88
-14
lines changed Original file line number Diff line number Diff line change @@ -99,7 +99,7 @@ int main(){
99
99
InsertBST (&bintree , n);
100
100
101
101
cout << " 中序遍历:" <<endl;
102
- InOrderTraversal (bintree);
102
+ // InOrderTraversal(bintree);
103
103
104
104
cin >>n;
105
105
cout <<" 输入要删除的数:" <<endl;
Original file line number Diff line number Diff line change 2
2
#include < stdlib.h>
3
3
using namespace std ;
4
4
5
+ /* *
6
+ * 增序链表合并
7
+ */
8
+
5
9
typedef struct List {
6
10
int num;
7
11
List *next;
@@ -16,6 +20,17 @@ void print(List *list){
16
20
cout <<" " <<endl;
17
21
}
18
22
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
+ */
19
34
List *MergeList (List *list , List *list2){
20
35
if (list == NULL )
21
36
return list2;
@@ -34,32 +49,91 @@ List *MergeList(List *list , List *list2){
34
49
return temp;
35
50
}
36
51
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 ;
44
86
for (int i = 10 ;i >= 2 ;i -= 2 ){
45
87
List *t = new List;
46
- t->next = l1 ->next ;
47
- l1 ->next = t;
88
+ t->next = (*l1) ->next ;
89
+ (*l1) ->next = t;
48
90
t->num = i;
49
91
}
50
92
51
93
for (int i = 11 ;i >= 3 ;i -= 2 ){
52
94
List *t = new List;
53
- t->next = l2 ->next ;
54
- l2 ->next = t;
95
+ t->next = (*l2) ->next ;
96
+ (*l2) ->next = t;
55
97
t->num = i;
56
98
}
99
+ }
57
100
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);
58
127
print (l1);
59
128
print (l2);
60
-
61
- cout << " ÅÅÐòºó" <<endl;
62
129
print (MergeList (l1 ,l2));
63
130
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
+
64
138
return 0 ;
65
139
}
You can’t perform that action at this time.
0 commit comments