带有文件保存和导入功能的用链表实现的通讯录

  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 }           
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的用链表实现通讯录管理系统的示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> // 定义通讯录结构体 typedef struct contact { char name[20]; char phone[20]; struct contact *next; } Contact; // 声明函数 void add_contact(Contact **head); void delete_contact(Contact **head); void search_contact(Contact *head); void display_contacts(Contact *head); void save_contacts(Contact *head); int main() { Contact *head = NULL; // 初始化头指针为空 int choice; while (1) { printf("\n1. 添加联系人\n"); printf("2. 删除联系人\n"); printf("3. 查找联系人\n"); printf("4. 显示所有联系人\n"); printf("5. 保存联系人到文件\n"); printf("0. 退出\n"); printf("请输入选项:"); scanf("%d", &choice); switch (choice) { case 1: add_contact(&head); break; case 2: delete_contact(&head); break; case 3: search_contact(head); break; case 4: display_contacts(head); break; case 5: save_contacts(head); break; case 0: exit(0); default: printf("无效选项,请重新输入!\n"); } } return 0; } // 添加联系人 void add_contact(Contact **head) { Contact *new_contact = (Contact *) malloc(sizeof(Contact)); printf("请输入姓名:"); scanf("%s", new_contact->name); printf("请输入电话号码:"); scanf("%s", new_contact->phone); new_contact->next = *head; *head = new_contact; printf("联系人添加成功!\n"); } // 删除联系人 void delete_contact(Contact **head) { char name[20]; printf("请输入要删除的联系人姓名:"); scanf("%s", name); Contact *prev = NULL; Contact *current = *head; while (current != NULL && strcmp(current->name, name) != 0) { prev = current; current = current->next; } if (current == NULL) { printf("未找到联系人!\n"); return; } if (prev == NULL) { *head = current->next; } else { prev->next = current->next; } free(current); printf("联系人删除成功!\n"); } // 查找联系人 void search_contact(Contact *head) { char name[20]; printf("请输入要查找的联系人姓名:"); scanf("%s", name); Contact *current = head; while (current != NULL) { if (strcmp(current->name, name) == 0) { printf("姓名:%s,电话:%s\n", current->name, current->phone); return; } current = current->next; } printf("未找到联系人!\n"); } // 显示所有联系人 void display_contacts(Contact *head) { if (head == NULL) { printf("通讯录为空!\n"); return; } Contact *current = head; while (current != NULL) { printf("姓名:%s,电话:%s\n", current->name, current->phone); current = current->next; } } // 保存联系人到文件 void save_contacts(Contact *head) { if (head == NULL) { printf("通讯录为空!\n"); return; } FILE *fp = fopen("contacts.txt", "w"); if (fp == NULL) { printf("保存失败!\n"); return; } Contact *current = head; while (current != NULL) { fprintf(fp, "%s,%s\n", current->name, current->phone); current = current->next; } fclose(fp); printf("保存成功!\n"); } ``` 这个示例代码实现了添加、删除、查找、显示、保存联系人的功能,其中通讯录使用链表来存储,每个节点包括姓名、电话和下一个节点的指针。在添加联系人时,新的节点会插入到链表的头部;在删除联系人时,需要遍历链表找到要删除的节点并将其从链表中删除;在查找联系人时,也需要遍历链表并按姓名进行比较;在显示所有联系人时,只需要遍历链表并输出每个节点的信息;在保存联系人到文件时,可以使用文件 I/O 将每个节点的信息写入到一个文件中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值