单向链表实例:终端交互简易通讯录

  1 #include    <stdio.h>
  2 #include    <stdlib.h>
  3 #include    <string.h>
  4 
  5 
  6 typedef struct Contacts_infomation{
  7     char name[13];
  8     char work_unit[61];
  9     char phone_number[12];
 10     char E_mail[61];
 11     struct Contacts_infomation *next;
 12 }con_info;
 13 
 14 
 15 con_info * Creat_node(void)
 16 {
 17     con_info *new;
 18 
 19     new = (con_info *)malloc(sizeof(con_info));
 20     if(!new){
 21         printf("Malloc Error!
");
 22         exit(-1);
 23     }    
 24     new->next = NULL;
 25     
 26     return new;
 27 }
 28 
 29 int insert_node(con_info ** phead)
 30 {
 31     con_info *new, *cur;    
 32 
 33     cur = *phead;
 34     new = Creat_node();
 35     if(!new){
 36         return -1;
 37     }    
 38     
 39     printf("please input name,work unit,phone number and E-mail (separated by enter):
");
 40     printf("name:");
 41     scanf("%s", new->name);
 42     printf("work unit:");
 43     scanf("%s", new->work_unit);
 44     printf("phone number:");
 45     scanf("%s", new->phone_number);
 46     printf("E-mail:");
 47     scanf("%s", new->E_mail);
 48     
 49     if(!(*phead)){
 50         *phead = new;
 51         return 0;
 52     }
 53     if(strncmp((*phead)->name,new->name,3) > 0){
 54         new->next = *phead;
 55         *phead = new;
 56         return 0;
 57     }
 58     while(cur->next){
 59         if(strncmp(cur->next->name, new->name,3) > 0){
 60             new->next = cur->next;
 61             cur->next = new;
 62             return 0;
 63         }
 64     }
 65     cur->next = new;
 66 
 67     return 0;
 68 }
 69 int  del_node(con_info **phead){
 70     con_info *cur = *phead;
 71     char name[13];
 72 
 73     if(!cur){
 74         printf("Address book is empty
");
 75         return -1;
 76     }
 77     printf("please input want delete name: ");
 78     scanf("%s", name);
 79 
 80     if(strcasecmp((*phead)->name, name) == 0){
 81         con_info *tmp = (*phead)->next;
 82         free(*phead);
 83         *phead = tmp;
 84         return 0;
 85     }
 86     while(cur->next){
 87         if(strcmp(cur->next->name, name) == 0){
 88             con_info *tmp = cur->next;
 89             cur->next = tmp->next;
 90             free(tmp);
 91             return 0;
 92         }
 93         cur = cur->next;
 94     }
 95     getchar();
 96     printf("

Can't find %s
", name);
 97     printf("

Please input Enter continue...");
 98     char ch = getchar();
 99 
100     return 0;    
101 }
102 
103 int mod_node(con_info **phead){
104     con_info *cur =  *phead;
105     char name[13];
106     char reply[10];
107 
108     if(!cur){
109         printf("Address book is empty
");
110         return -1;
111     }
112     printf("Please input want modify name: ");
113     scanf("%s", name);
114     while(cur){
115         if(strcasecmp(cur->name, name) == 0){
116 
117             printf("Modify name?(y/any key): ");
118             scanf("%s", reply);
119             if(strcmp(reply, "y") == 0){
120                 printf("Please new input name: ");            
121                 scanf("%s", cur->name);
122             }
123             printf("Modify work unit?(y/any key): ");
124             scanf("%s", reply);
125             if(strcmp(reply, "y") == 0){
126                 printf("Please input new work_unit: ");
127                 scanf("%s", cur->work_unit);
128             }
129             printf("Modify phone number?(y/any key): ");
130             scanf("%s", reply);
131             if(strcmp(reply, "y") == 0){
132                 printf("Please input new phone number: ");
133                 scanf("%s", cur->phone_number);
134             }
135             printf("Modify E-mail?(y/any key): ");
136             scanf("%s", reply);
137             if(strcmp(reply, "y") == 0){
138                 printf("Please input new E-mail: ");
139                 scanf("%s", cur->E_mail);
140             }
141             return 0;
142         }
143     cur = cur->next;
144     }
145     getchar();
146     printf("No contact was found.
");
147     printf("

Please input Enter continue...");
148     char ch = getchar();
149     return 0;
150 }
151 
152 int search_node(con_info **phead){
153     con_info *cur = *phead;
154     char in[13];
155     if(!cur){
156         printf("Address book is empty
");
157         return -1;
158     }
159     printf("Please input name or phone number: ");
160     scanf("%s", in);
161 
162     while(cur){
163         if(strcasecmp(cur->name, in) == 0 || strcmp(cur->phone_number, in) == 0){
164             printf("name		work unit							phone number		E-mail
");
165             printf("%-16s%-64s%-24s%-30s
", cur->name, cur->work_unit, cur->phone_number, cur->E_mail);
166             getchar();
167             printf("Please input Enter continue...");
168             char ch = getchar();
169 
170             return 0;
171         }
172         cur = cur->next;
173     }
174     printf("

Can't find %s
", in);
175     getchar();
176     printf("Please input Enter continue...");
177     char ch = getchar();
178     
179     return 0;
180 }
181 int print_con(con_info *phead){
182     con_info *cur = phead;
183     
184     printf("name		work unit							phone number		E-mail
");
185     if(!cur){
186         printf("NULL
");
187         return -1;
188     }
189     while(cur){
190         printf("%-16s%-64s%-24s%-30s
", cur->name, cur->work_unit, cur->phone_number, cur->E_mail);
191         cur = cur->next;
192     }
193     getchar();
194     printf("

Please input Enter continue...");
195     char ch = getchar();
196 
197     return 0;
198 }
199 
200 
201 
202 
203 int main(void)
204 {
205     con_info *head = NULL;
206     char op[20];
207     char dd[20];
208 
209     do{
210         printf("


1.New contacts
2.Delete contact
3.Unfoling contacts
4.Modify contacts
5.serch
6.Exit

8.Clean screen

********************************


");
211         printf("Please inpu op: ");
212         scanf("%s", op);
213         printf("

");
214         if(!strcmp(op,"1")){    
215             insert_node(&head);
216         }else if(!strcmp(op, "2")){
217             del_node(&head);
218         }else if(!strcmp(op, "3")){
219             print_con(head);
220         }else if(!strcmp(op, "4")){
221             mod_node(&head);
222         }else if(!strcmp(op, "5")){
223             search_node(&head);
224         }else if(!strcmp(op, "6")){
225             break;
226         }else if(!strcmp(op, "8")){
227             //printf("33[2J") ;
228             system("clear");
229         }
230         else{
231             printf("Ipunt opearton error
");
232         }
233     }while(1);        
234         
235     return 0;
236 }


由于通讯录不怎么需要倒序操作,又是一个简易的,所以干脆就使用了单向链表。Running demo:

交互界面(^_^简易的,所以特别简陋):

各个功能演示一遍:

原文地址:https://www.cnblogs.com/BMing/p/10003247.html