@@ -11,31 +11,54 @@ struct Person
1111 char phone [40 ];
1212 struct Person * next ;
1313};
14+
15+ void printmenu ();
1416void getInput (struct Person * person );
1517void printPerson (struct Person * person );
18+
19+ /*
20+ malloc failed -> -1 -> exit(1);
21+ Success -> 0
22+ */
1623int addPerson (struct Person * * contacts );
24+
25+ /*
26+ Not Found -> -1
27+ Success -> 0
28+ */
1729int changePerson (struct Person * contacts );
30+
31+ /*
32+ Not Found -> -1
33+ Success -> 0
34+ */
1835int delPerson (struct Person * * contacts );
36+
37+ /*
38+ Not Found -> NULL
39+ Success -> struct Person*
40+ */
1941struct Person * findPerson (struct Person * contacts );
42+
2043void displayContacts (struct Person * contacts );
44+
2145void releaseContacts (struct Person * * contacts );
46+
2247int main ()
2348{
2449 short code ;
2550 struct Person * contacts = NULL ;
26- printf ("|Welcome to ContactsBook Manager Program|\n" );
27- printf ("|1:insert new Contact-------------------|\n" );
28- printf ("|2:find for existing Contact------------|\n" );
29- printf ("|3.change existing Contact information--|\n" );
30- printf ("|4.delete existing Contact--------------|\n" );
31- printf ("|5.display all Contact------------------|\n" );
32- printf ("|6.exit---------------------------------|\n" );
51+ printmenu ();
3352 while (1 )
3453 {
3554 printf ("Please input the command code:\n" );
3655 scanf ("%d" , & code );
3756 switch (code )
3857 {
58+ case 0 : {
59+ printmenu ();
60+ break ;
61+ }
3962 case 1 : {
4063 switch (addPerson (& contacts ))
4164 {
@@ -114,6 +137,7 @@ void getInput(struct Person *person)
114137 printf ("Please input name:\n" );
115138 scanf ("%s" , person -> name );
116139 // Bounds Check Elimination is required
140+
117141 printf ("Please input phone number:\n" );
118142 scanf ("%s" , person -> phone );
119143 // Bounds Check Elimination is required
@@ -125,18 +149,15 @@ void printPerson(struct Person *person)
125149 printf ("Phone number:\n%s\n" , person -> phone );
126150}
127151
128- /*
129- malloc failed -> -1 -> exit(1);
130- Success -> 0
131- */
132152int addPerson (struct Person * * contacts )
133153{
134154 struct Person * person = (struct Person * )malloc (sizeof (struct Person ));
155+
135156 if (person == NULL )
136157 {
137158 return -1 ;
138159 }
139-
160+
140161 getInput (person );
141162
142163 // linked-list is not empty
@@ -153,44 +174,27 @@ int addPerson(struct Person **contacts)
153174 return 0 ;
154175}
155176
156- /*
157- Not Found -> NULL
158- Success -> struct Person*
159- */
160177struct Person * findPerson (struct Person * contacts )
161178{
162- // printf("Please input the name:\n");
163179 char temp [40 ];
164180 // This should be move to function param
181+
165182 scanf ("%s" , temp );
166183 // Bounds Check Elimination is required
184+
167185 struct Person * current = contacts ;
168186 while (current != NULL && strcmp (current -> name , temp ))
169187 {
170188 current = current -> next ;
171189 }
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- // }
183190
184191 return current ;
185192}
186193
187- /*
188- Not Found -> -1
189- Success -> 0
190- */
191194int changePerson (struct Person * contacts )
192195{
193196 struct Person * current = findPerson (contacts );
197+
194198 if (current != NULL )
195199 {
196200 printf ("Please input new Phone number:\n" );
@@ -204,13 +208,10 @@ int changePerson(struct Person *contacts)
204208 }
205209}
206210
207- /*
208- Not Found -> -1
209- Success -> 0
210- */
211211int delPerson (struct Person * * contacts )
212212{
213213 struct Person * person = findPerson (* contacts );
214+
214215 if (person == NULL )
215216 {
216217 return -1 ;
@@ -237,6 +238,7 @@ int delPerson(struct Person **contacts)
237238 return 0 ;
238239 }
239240}
241+
240242void displayContacts (struct Person * contacts )
241243{
242244 while (contacts != NULL )
@@ -245,13 +247,27 @@ void displayContacts(struct Person *contacts)
245247 contacts = contacts -> next ;
246248 }
247249}
250+
248251void releaseContacts (struct Person * * contacts )
249252{
250253 struct Person * temp = * contacts ;
254+
251255 while (* contacts != NULL )
252256 {
253257 temp = * contacts ;
254258 * contacts = (* contacts )-> next ;
255259 free (temp );
256260 }
257- }
261+ }
262+
263+ void printmenu ()
264+ {
265+ printf ("|Welcome to ContactsBook Manager Program|\n" );
266+ printf ("|0:print the Menu-----------------------|\n" );
267+ printf ("|1:insert new Contact-------------------|\n" );
268+ printf ("|2:find for existing Contact------------|\n" );
269+ printf ("|3.change existing Contact information--|\n" );
270+ printf ("|4.delete existing Contact--------------|\n" );
271+ printf ("|5.display all Contact------------------|\n" );
272+ printf ("|6.exit---------------------------------|\n" );
273+ }
0 commit comments