@@ -13,9 +13,9 @@ struct Person
1313};
1414void getInput (struct Person * person );
1515void printPerson (struct Person * person );
16- void addPerson (struct Person * * contacts );
17- void changePerson (struct Person * contacts );
18- void delPerson (struct Person * * contacts );
16+ int addPerson (struct Person * * contacts );
17+ int changePerson (struct Person * contacts );
18+ int delPerson (struct Person * * contacts );
1919struct Person * findPerson (struct Person * contacts );
2020void displayContacts (struct Person * contacts );
2121void releaseContacts (struct Person * * contacts );
@@ -37,10 +37,21 @@ int main()
3737 switch (code )
3838 {
3939 case 1 : {
40- addPerson (& contacts );
40+ switch (addPerson (& contacts ))
41+ {
42+ case -1 : {
43+ printf ("ERROR: malloc failed" );
44+ exit (1 );
45+ break ;
46+ }
47+ case 0 : {
48+ break ;
49+ }
50+ }
4151 break ;
4252 }
4353 case 2 : {
54+ printf ("Please input the name:\n" );
4455 struct Person * person = findPerson (contacts );
4556 if (person != NULL )
4657 {
@@ -53,11 +64,32 @@ int main()
5364 break ;
5465 }
5566 case 3 : {
56- changePerson (contacts );
67+ printf ("Please input the name:\n" );
68+ switch (changePerson (contacts ))
69+ {
70+ case -1 : {
71+ printf ("The person is not found\n" );
72+ break ;
73+ }
74+ case 0 : {
75+ break ;
76+ }
77+ }
78+
5779 break ;
5880 }
5981 case 4 : {
60- delPerson (& contacts );
82+ printf ("Please input the name:\n" );
83+ switch (delPerson (& contacts ))
84+ {
85+ case -1 : {
86+ printf ("The person is not found\n" );
87+ break ;
88+ }
89+ case 0 : {
90+ break ;
91+ }
92+ }
6193 break ;
6294 }
6395 case 5 : {
@@ -76,6 +108,7 @@ int main()
76108END :
77109 releaseContacts (& contacts );
78110}
111+
79112void getInput (struct Person * person )
80113{
81114 printf ("Please input name:\n" );
@@ -85,22 +118,30 @@ void getInput(struct Person *person)
85118 scanf ("%s" , person -> phone );
86119 // Bounds Check Elimination is required
87120}
121+
88122void printPerson (struct Person * person )
89123{
90124 printf ("Name:\n%s\n" , person -> name );
91125 printf ("Phone number:\n%s\n" , person -> phone );
92126}
93- void addPerson (struct Person * * contacts )
127+
128+ /*
129+ malloc failed -> -1 -> exit(1);
130+ Success -> 0
131+ */
132+ int addPerson (struct Person * * contacts )
94133{
95134 struct Person * person = (struct Person * )malloc (sizeof (struct Person ));
96135 if (person == NULL )
97136 {
98- printf ("ERROR: malloc failed" );
99- exit (1 );
137+ return -1 ;
100138 }
101- if (contacts != NULL )
139+
140+ getInput (person );
141+
142+ // linked-list is not empty
143+ if (* contacts != NULL )
102144 {
103- getInput (person );
104145 person -> next = * contacts ;
105146 * contacts = person ;
106147 }
@@ -109,56 +150,70 @@ void addPerson(struct Person **contacts)
109150 * contacts = person ;
110151 person -> next = NULL ;
111152 }
153+ return 0 ;
112154}
155+
156+ /*
157+ Not Found -> NULL
158+ Success -> struct Person*
159+ */
113160struct Person * findPerson (struct Person * contacts )
114161{
115- printf ("Please input the name:\n" );
116- char temp [20 ];
162+ // printf("Please input the name:\n");
163+ char temp [40 ];
164+ // This should be move to function param
117165 scanf ("%s" , temp );
118166 // Bounds Check Elimination is required
119167 struct Person * current = contacts ;
120168 while (current != NULL && strcmp (current -> name , temp ))
121169 {
122170 current = current -> next ;
123171 }
124- if (current != NULL )
125- {
126- printf ("Name:\n%s\n" , current -> name );
127- printf ("Phone number:\n%s\n" , current -> phone );
128- // Maybe this should show in return value instead
129- }
130- else
131- {
132- printf ("Not Found\n" );
133- // Maybe this should show in return value instead
134- }
172+ // if (current != NULL)
173+ // {
174+ // printf("Name:\n%s\n", current->name);
175+ // printf("Phone number:\n%s\n", current->phone);
176+ // // Maybe this should show in return value instead
177+ // }
178+ // else
179+ // {
180+ // printf("Not Found\n");
181+ // // Maybe this should show in return value instead
182+ // }
135183
136184 return current ;
137185}
138- void changePerson (struct Person * contacts )
186+
187+ /*
188+ Not Found -> -1
189+ Success -> 0
190+ */
191+ int changePerson (struct Person * contacts )
139192{
140193 struct Person * current = findPerson (contacts );
141194 if (current != NULL )
142195 {
143196 printf ("Please input new Phone number:\n" );
144- // Maybe this should be wrote in main function instead
145197 scanf ("%s" , current -> phone );
146198 // Bounds Check Elimination is required
147- printf ("Done" );
148- // Maybe this should show in return value instead
199+ return 0 ;
149200 }
150201 else
151202 {
152- printf ("Not Found\n" );
153- // Maybe this should show in return value instead
203+ return -1 ;
154204 }
155205}
156- void delPerson (struct Person * * contacts )
206+
207+ /*
208+ Not Found -> -1
209+ Success -> 0
210+ */
211+ int delPerson (struct Person * * contacts )
157212{
158213 struct Person * person = findPerson (* contacts );
159214 if (person == NULL )
160215 {
161- printf ( "Not Found" ) ;
216+ return -1 ;
162217 }
163218 else
164219 {
@@ -179,6 +234,7 @@ void delPerson(struct Person **contacts)
179234 current -> next = current -> next -> next ;
180235 free (target );
181236 }
237+ return 0 ;
182238 }
183239}
184240void displayContacts (struct Person * contacts )
0 commit comments