-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeList.cc
151 lines (129 loc) · 3.62 KB
/
MergeList.cc
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct ListNode
{
int nValue;
struct ListNode *pNext;
} ListNode;
ListNode* MergeList(ListNode *pHead1, ListNode *pHead2);
ListNode* MergeList2(ListNode *pHead1, ListNode *pHead2);
void FreeList(ListNode *pHead);
int main()
{
int num1, num2;
while(scanf("%d%d", &num1, &num2) != EOF){
ListNode *pHead1 = NULL;
ListNode *pHead2 = NULL;
//初始化第一条链表
ListNode *pTail1;
for(int i = 0; i < num1; i++){
if(pHead1 == NULL){
pHead1 = (ListNode*) malloc(sizeof(ListNode));
if(scanf("%d", &(pHead1->nValue)) < 1)
perror("input error");
pHead1->pNext = NULL;
pTail1 = pHead1;
continue;
}
pTail1->pNext = (ListNode*)malloc(sizeof(ListNode));
pTail1 = pTail1->pNext;
pTail1->pNext = NULL;
if(scanf("%d", &(pTail1->nValue)) < 1)
perror("input error");
}
//初始化第二条链表
ListNode *pTail2;
for(int i = 0; i < num2; i++){
if(pHead2 == NULL){
pHead2 = (ListNode*)malloc(sizeof(ListNode));
if(scanf("%d", &(pHead2->nValue)) < 1)
perror("input error");
pHead2->pNext = NULL;
pTail2 = pHead2;
continue;
}
pTail2->pNext = (ListNode*)malloc(sizeof(ListNode));
pTail2 = pTail2->pNext;
pTail2->pNext = NULL;
if(scanf("%d", &(pTail2->nValue)) < 1)
perror("input error");
}
//合并链表
ListNode* pMergedList = MergeList2(pHead1, pHead2);
//打印合并后的链表
if(pMergedList == NULL){
printf("NULL\n");
continue;
}
ListNode* pNode = pMergedList;
while(pNode != NULL){
printf("%d", pNode->nValue);
if(pNode->pNext != NULL)
printf(" ");
pNode = pNode->pNext;
}
printf("\n");
//释放链表空间
FreeList(pMergedList);
}
}
void FreeList(ListNode *pHead)
{
ListNode *pNode = pHead;
while(pHead){
pNode = pHead->pNext;
free((void*)pHead);
pHead = pNode;
}
}
/*
* 这种做法是存在安全隐患的做法:
* pMergedHead自己分配了空间,可以不改变pHead1和pHead2的结构,
* 但是pMergedHead同时又参杂了pHead1和pHead2的元素
* 这样会导致无法释放pMergedList占用的空间,例如:
* free(pHead1);
* free(pHead2);
* free(pMergedHead);
* 这样在释放pMergedHead所占空间的时候,有可能重复释放pHead1或pHead2中
* 已经释放过的空间,引发Runtime Error
*/
ListNode* MergeList(ListNode *pHead1, ListNode *pHead2)
{
if(pHead1 == NULL)
return pHead2;
else if(pHead2 == NULL)
return pHead1;
ListNode *pMergedHead = (ListNode*)malloc(sizeof(ListNode));
if(pHead1->nValue <= pHead2->nValue){
pMergedHead->nValue = pHead1->nValue;
pMergedHead->pNext = MergeList(pHead1->pNext, pHead2);
}
else{
pMergedHead->nValue = pHead2->nValue;
pMergedHead->pNext = MergeList(pHead1, pHead2->pNext);
}
return pMergedHead;
}
/*
* 不为pMergedHead分配新空间的方法,
* 这样只需释放pMergedHead占的空间即相当于同时释放了pHead1和pHead2所占的空间;
* 但是这样会改变pHead1和pHead2的链表结构
*/
ListNode* MergeList2(ListNode *pHead1, ListNode *pHead2)
{
if(pHead1 == NULL)
return pHead2;
else if(pHead2 == NULL)
return pHead1;
ListNode *pMergedHead = NULL;
if(pHead1->nValue <= pHead2->nValue){
pMergedHead = pHead1;
pMergedHead->pNext = MergeList2(pHead1->pNext, pHead2);
}
else{
pMergedHead = pHead2;
pMergedHead->pNext = MergeList2(pHead1, pHead2->pNext);
}
return pMergedHead;
}