1 #include<stdio.h>
2 #include<stdlib.h>
3 #include<string.h>
4 struct student//学生类
5 {
6 char num[40];
7 char name[40];
8 char qq[40];
9 char mobilephone[40];
10 struct student * next;
11 };
12 typedef struct student Node;
13 typedef struct student * Link;
14 void ui()//界面
15 {
16 printf("*******************************\n");
17 printf("************Welcome************\n");
18 printf("*******************************\n");
19 printf("1 输入信息\n");
20 printf("2 显示信息\n");
21 printf("3 查找信息\n");
22 printf("4 删除信息\n");
23 printf("5 删除所有信息\n");
24 printf("6 修改信息\n");
25 printf("7 保存\n");
26 printf("8 退出\n");
27 }
28 void create_node(Link * new_node)//创建结点
29 {
30 system("clear");
31 *new_node = (Link)malloc(sizeof(Node));
32 if (*new_node == NULL)//错误处理
33 {
34 printf("malloc error\n");
35 exit(-1);//正常退出0,非正常退出-1
36 }
37 }
38 void create_link(Link * head)//创建链表
39 {
40 create_node(head);
41 (*head)->next = NULL;
42 }
43 void insert_node_head(Link head,Link new_node)//输入学生信息,并排序插入
44 {
45 printf("请输入学号:\n");
46 scanf("%s",new_node->num);
47 if (strlen(new_node->num) !=9)
48 {
49 printf("学号长度不符!\n");
50 free(new_node);
51 return;
52 }
53 printf("请输入姓名:\n");
54 scanf("%s",new_node->name);
55 printf("请输入qq号:\n");
56 scanf("%s",new_node->qq);
57 printf("请输入手机号:\n");
58 scanf("%s",new_node->mobilephone);
59 if (strlen(new_node->mobilephone) !=11)
60 {
61 printf("手机长度不符!\n");
62 free(new_node);
63 return;
64 }
65 Link p,q;
66 p = q = head->next;
67 if (p == NULL)
68 {
69 head->next = new_node;
70 new_node->next = NULL;
71 }
72 else
73 {
74 while (p != NULL && strcmp((head->next)->name,new_node->name) < 0)
75 {
76 q = p;
77 p = p->next;
78 }
79 if (NULL == p)
80 {
81 q->next =new_node;
82 new_node->next = NULL;
83 }
84 else if (p ==q)
85 {
86 new_node->next = head->next;
87 head->next = new_node;
88 }
89 else
90 {
91 new_node->next=p;
92 q->next=new_node;
93 }
94 }
95 printf("运行成功\n");
96 }
97 void display(Link head)//输出
98 {
99 Link p;
100 p = head->next;
101 system("clear");
102 if (head == NULL)
103 {
104 printf ("link is not exist\n");
105 return;
106 }
107 if (NULL == p)
108 {
109 printf("Link is empty\n");
110 return;
111 }
112 while (p != NULL)
113 {
114 printf("学号是%s ",p->num);
115 printf("姓名是%s ",p->name);
116 printf("qq号是%s ",p->qq);
117 printf("手机号是%s \n",p->mobilephone);
118 p = p->next;
119 }
120 printf("输出成功\n");
121 }
122 void search(Link head)//查找
123 {
124 system("clear");
125 char search_name[40];
126 printf("请输入要查找学生的姓名:\n");
127 scanf("%s",search_name);
128 Link p;
129 p = head->next;
130 if (head->next == NULL)
131 {
132 printf("无学生信息!\n");
133 }
134 else
135 {
136 while (p != NULL && strcmp(p->name,search_name))
137 {
138 p = p->next;
139 }
140 if (NULL == p)
141 {
142 printf("不存在要查找的学生信息!\n");
143 }
144 else
145 {
146 printf("学号是%s ",p->num);
147 printf("姓名是%s ",p->name);
148 printf("qq号是%s ",p->qq);
149 printf("手机号是%s \n",p->mobilephone);
150 }
151 }
152 }
153 void delete(Link head)//删除
154 {
155 system("clear");
156 char delete_name[40];
157 printf("请输入要删除学生的姓名:\n");
158 scanf("%s",delete_name);
159 Link p,q;
160 p = q = head->next;
161 if (p == NULL)
162 {
163 printf("无学生信息\n");
164 }
165 else
166 {
167 while (p != NULL && strcmp(p->name,delete_name))
168 {
169 q = p;
170 p = p->next;
171 }
172 if (NULL == p)
173 {
174 printf("不存在要删除的学生信息\n");
175 }
176 else if (p == q)
177 {
178 head->next = p->next;
179 free(p);
180 printf("删除成功!\n");
181 }
182 else
183 {
184 q->next=p->next;
185 free(p);
186 printf("删除成功!\n");
187 }
188 }
189 }
190 void change(Link head)//修改
191 {
192 system("clear");
193 char change_name[40];
194 char change_num[40];
195 char change_mobilephone[40];
196 printf("请输入要修改学生的姓名:\n");
197 scanf("%s",change_name);
198 Link p;
199 p = head->next;
200 if (head->next == NULL)
201 {
202 printf("无学生信息!\n");
203 }
204 else
205 {
206 while (p != NULL && strcmp(p->name,change_name))
207 {
208 p = p->next;
209 }
210 if (NULL == p)
211 {
212 printf ("不存在要修改的学生信息!\n");
213 }
214 else
215 {
216 printf("请输入修改后的学号\n");
217 scanf("%s",change_num);
218 if (strlen(change_num) !=9)
219 {
220 printf("学号长度不符!\n");
221 return;
222 }
223 strcpy(p->num,change_num);
224 printf("请输入修改后的qq号\n");
225 scanf("%s",p->qq);
226 printf("请输入修改后的手机号\n");
227 scanf("%s",change_mobilephone);
228 if (strlen(change_mobilephone) !=11)
229 {
230 printf("手机长度不符!\n");
231 return;
232 }
233 strcpy(p->mobilephone,change_mobilephone);
234 printf("修改成功\n");
235 }
236 }
237 }
238 void make_empty(Link head)//清空
239 {
240 system("clear");
241 Link p;
242 p = head->next;
243 if(head->next == NULL)
244 {
245 printf("Link is empty\n");
246 }
247 else
248 {
249 while(head->next != NULL)
250 {
251 head->next = p->next;
252 free(p);
253 p =head->next;
254 }
255 }
256 }
257 void save(Link head)//保存
258 {
259 system("clear");
260 FILE *fp;
261 Link p;
262 if (head->next == NULL)
263 {
264 printf("无需要保存信息");
265 return;
266 }
267 p = head->next;
268 if ((fp=fopen("text","w+")) == NULL)
269 {
270 printf("file open error!\n");
271 exit(0);
272 }
273 while(p != NULL)
274 {
275 fwrite(p,sizeof(Node),1,fp);
276 p = p->next;
277 }
278 fclose(fp);
279 printf("保存成功\n");
280 }
281 void load(Link head)//导入
282 {
283 FILE *fp;
284 Link p;
285 Link q;
286 q = head;
287 char ch;
288 if ((fp=fopen("text","r")) == NULL)
289 {
290 printf("file open error!\n");
291 exit(0);
292 }
293 while((ch = fgetc(fp)) != EOF)
294 {
295 fseek(fp,-1L,SEEK_CUR);
296 create_node(&p);
297 fread(p,sizeof(Node),1,fp);
298 q->next = p;
299 p->next = NULL;
300 q = p;
301 }
302 fclose(fp);
303 }
304 int main()
305 {
306 int i;
307 Link head;
308 Link new_node;
309 create_link(&head);
310 load(head);
311 while(1)
312 {
313 ui();
314 printf("请输入你要进行操作的序号:\n");
315 scanf("%d",&i);
316 switch(i)
317 {
318 case 1:create_node(&new_node);insert_node_head(head,new_node);break;
319 case 2:display(head);break;
320 case 3:search(head);break;
321 case 4:delete(head);break;
322 case 5:make_empty(head);break;
323 case 6:change(head);break;
324 case 7:save(head);break;
325 case 8:exit(0);
326 default:exit(-1);
327 }
328
329 }
330 return 0;
331 }
带有文件保存和导入功能的用链表实现的通讯录
最新推荐文章于 2022-04-09 17:37:09 发布