c语言 链表 写通讯录
程序员文章站
2022-08-09 19:31:40
/* 实现一个通讯录;
通讯录可以用来存储1000个人的信息,每个人的信息包括:
姓名、性别、年龄、电话、住址
提供方法:
1. 添加联系人信息
2. 删除指定联系...
/* 实现一个通讯录; 通讯录可以用来存储1000个人的信息,每个人的信息包括: 姓名、性别、年龄、电话、住址 提供方法: 1. 添加联系人信息 2. 删除指定联系人信息 3. 查找指定联系人信息 4. 修改指定联系人信息 5. 显示所有联系人信息 6. 清空所有联系人 */ #include<stdio.h> #include"phone_book.h" #include<string.h> #include<assert.h> typedef struct phone_book { char name[10]; char sex[5]; char addr[20]; int year; int phone_num; struct phone_book *pnext; }book,*pbook; pbook add_linkman(pbook head) { int i = 0; char name[10]; char sex[5]; char addr[20]; int year; long phone_num; pbook pbase = head; assert(head); flag: printf("你要添加多少个联系人:"); scanf("%d",&i); if (i <= 0) { printf("输入数量错误!\n"); goto flag; } if (head->pnext == null) { while (i--) { pbook pnew = (pbook)malloc(sizeof(book)); assert(pnew); printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(pnew->name, name); strcpy(pnew->sex, sex); strcpy(pnew->addr, addr); scanf("%d", &pnew->year); scanf("%d", &pnew->phone_num); pbase->pnext = pnew; pnew->pnext = null; pbase = pnew; } } else { while (pbase->pnext != null) { pbase = pbase->pnext; } while (i--) { pbook pnew = (pbook)malloc(sizeof(book)); assert(pnew); printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(pnew->name, name); strcpy(pnew->sex, sex); strcpy(pnew->addr, addr); scanf("%d", &pnew->year); scanf("%d", &pnew->phone_num); pbase->pnext = pnew; pnew->pnext = null; } } return head; } pbook remove_linkman(pbook head) { pbook link_man = head; char name[10]; if (!head) { printf("没有联系人,不能删除!\n"); return null; } printf("请问你要删除哪位联系人? "); scanf("%s",name); while (link_man->pnext!=null) { if (strcmp(link_man->pnext->name, name) == 0) { if (link_man->pnext->pnext == null) { free(link_man->pnext); link_man->pnext = null; printf("删除完成!\n"); } else { pbook ptmp = null; ptmp = link_man->pnext; link_man->pnext = link_man->pnext->pnext; free(ptmp); printf("删除完成!\n"); } return head; } else { link_man = link_man->pnext; } } printf("没有找到该联系人!\n"); return head; } void traver_linkman(pbook head) { pbook link_man = head; if (head->pnext == null) { printf("电话簿为空!\n"); } while (link_man->pnext!=null) { printf("%s\n", link_man->pnext->name); printf("%s\n", link_man->pnext->sex); printf("%s\n", link_man->pnext->addr); printf("%d\n", link_man->pnext->year); printf("%ld\n", link_man->pnext->phone_num); printf("***********************************\n"); link_man = link_man->pnext; } } void empty_linkman(pbook head) { pbook ptmp = null; pbook link_man = head; if (head->pnext == null) { printf("电话簿为空!不用清空哦!(ˇ0ˇ) \n"); } while (link_man->pnext != null) { if (link_man->pnext->pnext == null) { free(link_man->pnext); link_man->pnext = null; } else { ptmp = link_man->pnext; link_man->pnext = link_man->pnext->pnext; free(ptmp); } } printf("清空完成!\n"); } pbook amend_linkman(pbook head) { char name[10]; char sex[5]; char addr[20]; pbook link_man = head; if (!head) { printf("没有联系人,不能修改!\n"); return null; } printf("请问你要修改哪位联系人? "); scanf("%s", name); while (link_man->pnext != null) { if (strcmp(link_man->pnext->name, name) == 0) { printf("请输入联系人信息:(姓名、性别、地址、年龄、电话号码)\n"); scanf("%s", name); scanf("%s", sex); scanf("%s", addr); strcpy(link_man->pnext->name, name); strcpy(link_man->pnext->sex, sex); strcpy(link_man->pnext->addr, addr); scanf("%d", &link_man->pnext->year); scanf("%d", &link_man->pnext->phone_num); return head; } else link_man = link_man->pnext; } return head; } void search_linkman(pbook head) { char name[10]; pbook link_man = head; if (!head) { printf("没有联系人,查找失败!\n"); return null; } printf("请问你要查找哪位联系人? "); scanf("%s", name); while (link_man->pnext != null) { if (strcmp(link_man->pnext->name, name) == 0) { printf("%s\n", link_man->pnext->name); printf("%s\n", link_man->pnext->sex); printf("%s\n", link_man->pnext->addr); printf("%d\n", link_man->pnext->year); printf("%ld\n", link_man->pnext->phone_num); printf("***********************************\n"); return 0; } else link_man = link_man->pnext; } printf("没有找到!\n"); } void start(pbook *phead) { int i = -1; assert(phead); while (1) { printf("欢迎使用电话薄系统!选择您的操作:\n\n"); printf("*********************************************\n" "*1.添加联系人信息 2.删除指定联系人信息 *\n" "*3.查找指定联系人信息 4.修改指定联系人信息 *\n" "*5.显示所有联系人信息 6.清空所有联系人 *\n" "*0.结束系统! ————中情局专用———— *\n" "*********************************************\n"); scanf("%d",&i); switch (i) { case 0: printf("*****感谢使用本系统!*****\n"); exit(0); case 1: *phead=add_linkman( phead); break; case 2: *phead = remove_linkman(phead); break; case 3: search_linkman(phead); break; case 4: *phead = amend_linkman(phead); break; case 5: traver_linkman(phead); break; case 6: empty_linkman( phead); break; default: printf("选择有误!请重新选择\n"); break; } } } #include<stdio.h> #include"phone_book.h" int main() { pbook phead = null; start(&phead); return 0; }