Skip to content

Commit 54941e4

Browse files
committed
Use Perprocess to make code readable
1 parent 640cd97 commit 54941e4

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
lines changed

c/AddressBook/main.c

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#include <stdio.h>
55
#include <stdlib.h>
66
#include <string.h>
7-
// #define NOT_FOUND -1
8-
// #define MALLOC_FAILED -2
9-
// #define SUCCESS 0
7+
8+
#define NOT_FOUND NULL
9+
#define MALLOC_FAILED NULL
10+
#define END_OF_LIST NULL
11+
1012
struct Person
1113
{
1214
char name[20];
@@ -65,7 +67,7 @@ int main()
6567
}
6668
case 1: {
6769
struct Person *person = AddPerson(&contacts);
68-
if (person == NULL)
70+
if (person == MALLOC_FAILED)
6971
{
7072
printf("ERROR: malloc failed");
7173
exit(1);
@@ -75,7 +77,7 @@ int main()
7577
case 2: {
7678
printf("Please input the name:\n");
7779
struct Person *person = FindPerson(contacts);
78-
if (person != NULL)
80+
if (person != NOT_FOUND)
7981
{
8082
PrintPerson(person);
8183
}
@@ -88,7 +90,7 @@ int main()
8890
case 3: {
8991
printf("Please input the name:\n");
9092
struct Person *person = ChangePerson(contacts);
91-
if (person == NULL)
93+
if (person == NOT_FOUND)
9294
{
9395
printf("The person is not found\n");
9496
}
@@ -97,7 +99,7 @@ int main()
9799
case 4: {
98100
printf("Please input the name:\n");
99101
struct Person *person = DeletePerson(&contacts);
100-
if (person == NULL)
102+
if (person == NOT_FOUND)
101103
{
102104
printf("The person is not found\n");
103105
}
@@ -148,23 +150,26 @@ struct Person *AddPerson(struct Person **contacts)
148150

149151
if (person == NULL)
150152
{
151-
return NULL;
153+
return MALLOC_FAILED;
152154
}
153155

154156
GetInput(person);
155157

156-
// linked-list is not empty
157-
if (*contacts != NULL)
158+
// head insert
159+
if (*contacts != END_OF_LIST)
158160
{
161+
// linked-list is not empty
159162
person->next = *contacts;
160163
*contacts = person;
161164
}
162165
else
163166
{
167+
// linked-list is empty
164168
*contacts = person;
165169
person->next = NULL;
166170
}
167171
return person;
172+
// return the new-inserted person
168173
}
169174

170175
struct Person *FindPerson(struct Person *contacts)
@@ -176,7 +181,7 @@ struct Person *FindPerson(struct Person *contacts)
176181
// Bounds Check Elimination is required
177182

178183
struct Person *current = contacts;
179-
while (current != NULL && strcmp(current->name, temp))
184+
while (current != END_OF_LIST && strcmp(current->name, temp))
180185
{
181186
current = current->next;
182187
}
@@ -188,7 +193,7 @@ struct Person *ChangePerson(struct Person *contacts)
188193
{
189194
struct Person *current = FindPerson(contacts);
190195

191-
if (current != NULL)
196+
if (current != NOT_FOUND)
192197
{
193198
printf("Please input new Phone number:\n");
194199
scanf("%s", current->phone);
@@ -197,27 +202,26 @@ struct Person *ChangePerson(struct Person *contacts)
197202
}
198203
else
199204
{
200-
return NULL;
205+
return NOT_FOUND;
201206
}
202207
}
203208

204209
struct Person *DeletePerson(struct Person **contacts)
205210
{
206211
struct Person *person = FindPerson(*contacts);
207212

208-
if (person == NULL)
213+
if (person == NOT_FOUND)
209214
{
210-
return NULL;
215+
return NOT_FOUND;
211216
}
212217
else
213218
{
214219
struct Person *current = *contacts;
215220
if (current == person)
216221
{
222+
// target is the first node
217223
*contacts = current->next;
218-
// free(current);
219224
return current;
220-
// target is the first struct
221225
}
222226
else
223227
{
@@ -227,16 +231,14 @@ struct Person *DeletePerson(struct Person **contacts)
227231
}
228232
struct Person *target = current->next;
229233
current->next = current->next->next;
230-
// free(target);
231234
return target;
232235
}
233-
// return NULL;
234236
}
235237
}
236238

237239
void DisplayContacts(struct Person *contacts)
238240
{
239-
while (contacts != NULL)
241+
while (contacts != END_OF_LIST)
240242
{
241243
PrintPerson(contacts);
242244
contacts = contacts->next;
@@ -252,7 +254,7 @@ void ReleaseContacts(struct Person **contacts)
252254
{
253255
struct Person *temp = *contacts;
254256

255-
while (*contacts != NULL)
257+
while (*contacts != END_OF_LIST)
256258
{
257259
temp = *contacts;
258260
*contacts = (*contacts)->next;

0 commit comments

Comments
 (0)