经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言单链表实现通讯录管理系统
来源:jb51  时间:2021/5/31 13:04:42  对本文有异议

本文实例为大家分享了C语言单链表实现通讯录管理系统的具体代码,供大家参考,具体内容如下

本人前几天刚刚自学了单链表,趁热打铁,赶紧写一个小小的项目练练手。

单链表的实现在本人之前的博客中有:C语言编写一个链表

通讯录管理系统

保存人的信息有: 
名字   name
电话   telephone
性别   sex
年龄   age

用一个结构体来装这些信息:

  1. struct infor{
  2. char name[20];
  3. int age;
  4. char sex[8];
  5. char telephone[16];
  6. };

实现功能有:

增加联系人
删除联系人
修改联系人
寻找联系人
显示联系人

首先建立链表的基本功能创建头链表,创建节点,插入节点

  1. struct addre* Creathead(){ //创建头链表
  2. struct addre *headnode = (struct addre*)malloc(sizeof(struct addre));
  3. if (headnode == NULL){
  4. printf("malloc error\n");
  5. }
  6. headnode->next = NULL;
  7. return headnode;
  8.  
  9. }
  10.  
  11. struct addre* Creatlist(struct infor *list){ //创建节点
  12. struct addre *node = (struct addre*)malloc(sizeof(struct addre));
  13. if (node == NULL){
  14. printf("malloc error\n");
  15. }
  16. node->next = NULL;
  17. node->people.age = list->age;
  18. strcpy(node->people.name, list->name);
  19. strcpy(node->people.sex, list->sex);
  20. strcpy(node->people.telephone, list->telephone);
  21.  
  22. return node;
  23. }
  24.  
  25. void Addlist(struct addre *headnode,struct infor *list){ //插入节点
  26. struct addre *t = headnode;
  27. while (t->next != NULL){
  28. t = t->next;
  29. }
  30. struct addre *nodelist = Creatlist(list);
  31. t->next = nodelist;
  32. nodelist->next = NULL;
  33. }

然后在实现通讯录的功能

  1. void Addpeople(struct addre* node){ //添加人的信息
  2. struct infor *a=malloc(sizeof(struct infor)); // 创建动态信息结构体指针
  3. if (a == NULL){
  4. printf("malloc error\n");
  5. }
  6. printf("请输入名字\n");
  7. scanf("%s", a->name);
  8. printf("请输入年龄\n");
  9. scanf("%d", &a->age);
  10. printf("请输入性别\n");
  11. scanf("%s", a->sex);
  12. printf("请输入电话号码\n");
  13. scanf("%s", a->telephone);
  14. Addlist(node, a); //用尾插法插入该人信息
  15. printf("添加成功!\n");
  16. }
  17. void Deletepeople(struct addre *node){ //删除人的信息
  18. char *str = malloc(sizeof(char)* 10);
  19. if (str == NULL){ //通过名字寻找
  20. printf("malloc error\n");
  21. }
  22. printf("请输入要删除人的姓名\n");
  23. scanf("%s", str);
  24. struct addre *strat = node;
  25. struct addre *end = node->next;
  26. int flag = 0; //判断是否找到 0为未找到,1 找到
  27. while (end){ //判断end的 不然会越界
  28. if (strcmp(end->people.name, str) == 0){
  29. flag = 1;
  30. break;
  31. }
  32. node = node->next; //到下一个链表
  33. strat = node; //一个指向前面 一个指向后面,删除将end删除,前面那个直接指向end的指向
  34. end = node->next;
  35. }
  36. if (flag){
  37. strat->next = end->next;
  38. printf("删除成功\n");
  39. free(end);
  40. }
  41. else{
  42. printf("没找到!\n");
  43. }
  44. }
  45.  
  46. void Modifyinfor(struct addre *node){ //修改人的信息
  47. char *str = malloc(sizeof(char)* 10); //通过名字寻找
  48. if (str == NULL){
  49. printf("malloc error\n");
  50. }
  51. printf("请输入要修改人的姓名\n");
  52. scanf("%s", str);
  53. int flag = 0;
  54. while (node){
  55. if (strcmp(node->people.name, str) == 0){
  56. flag = 1;
  57. break;
  58. }
  59. node = node->next;
  60. }
  61. if (flag){
  62. printf("请重新输入该人信息\n");
  63. printf("请输入名字\n");
  64. scanf("%s", node->people.name);
  65. printf("请输入年龄\n");
  66. scanf("%d", &node->people.age);
  67. printf("请输入性别\n");
  68. scanf("%s", node->people.sex);
  69. printf("请输入电话号码\n");
  70. scanf("%s", node->people.telephone);
  71. printf("修改成功\n");
  72. }
  73. else{
  74. printf("没找到\n");
  75. }
  76. }
  77. void Foundpeople(struct addre *node){ //找到某人的信息并打印出来
  78. char *str = malloc(sizeof(char)* 10); //通过名字寻找
  79. if (str == NULL){
  80. printf("malloc error\n");
  81. }
  82. printf("请输入要查找人的姓名\n");
  83. scanf("%s", str);
  84. int flag = 0;
  85. while (node){
  86. if (strcmp(node->people.name, str) == 0){
  87. flag = 1;
  88. break;
  89. }
  90. node = node->next;
  91. }
  92. if (flag){
  93. printf("name\tage\tsex\ttelephone\n");
  94. printf("%s\t", node->people.name);
  95. printf("%d\t", node->people.age);
  96. printf("%s\t", node->people.sex);
  97. printf("%s\t", node->people.telephone);
  98. }
  99. else{
  100. printf("没找到!\n");
  101. }
  102. }
  103.  
  104. void Display(struct addre *node){
  105. struct addre *pmove = node->next; //要从头节点的下一个节点显示信息
  106. printf("name\tage\tsex\ttelephone\n");
  107. while (pmove){
  108. printf("%s\t%d\t%s\t%s\n", pmove->people.name, pmove->people.age, pmove->people.sex, pmove->people.telephone);
  109. pmove = pmove->next;
  110. }
  111. }

其它代码

菜单:

  1. void Menu(){
  2. printf("+-----------------+\n");
  3. printf("+-1.add 2.delet-+\n");
  4. printf("+-3.modify 4.seek-+\n");
  5. printf("+-5.display6.exit-+\n");
  6. printf("+-----------------+\n");
  7. printf("Please Enter Your Select\n");
  8. }

本人使用的时多文件的方式上面的代码都在函数定义的源文件里实现

main函数的源文件代码:

  1. #include"addressbank.h"
  2.  
  3. /***********************
  4. 信息:
  5. 名字 name
  6. 年龄 age
  7. 电话 telephone
  8. 性别 sex
  9. 功能:
  10. 增加
  11. 删除
  12. 修改
  13. 寻找
  14. 打印
  15. 显示
  16. ***********************/
  17. int main(){
  18. struct addre* node = Creathead();
  19. int quit = 0;
  20. while (!quit){
  21. Menu();
  22. int select = 0;
  23. scanf("%d", &select);
  24. switch (select){
  25. case 1:
  26. Addpeople(node);
  27. break;
  28. case 2:
  29. Deletepeople(node);
  30. break;
  31. case 3:
  32. Modifyinfor(node);
  33. break;
  34. case 4:
  35. Foundpeople(node);
  36. break;
  37. case 5:
  38. Display(node);
  39. break;
  40. case 6:
  41. quit = 1;
  42. break;
  43. default:
  44. printf("Enter Error!\n");
  45. break;
  46. }
  47. }
  48. system("pause");
  49. return 0;
  50. }

声明的头文件:

  1. #ifndef __ADDRESSBANK_H__
  2. #define __ADDRESSBANK_H__
  3.  
  4. #include<stdio.h>
  5. #include<string.h>
  6. struct infor{//信息结构体
  7. char name[20];
  8. int age;
  9. char sex[8];
  10. char telephone[16];
  11. };
  12. struct addre{ //链表
  13. struct infor people;
  14. struct addre *next;
  15. };
  16. //功能函数
  17. extern void Menu();
  18. extern void Addpeople(struct addre *node);
  19. extern void Deletepeople(struct addre *node);
  20. extern void Modifyinfor(struct addre *node);
  21. extern void Foundpeople(struct addre *node);
  22. extern void Display(struct addre *node);
  23. //下面未链表函数
  24. extern struct addre* Creatlist(struct infor *list);
  25. extern struct addre* Creathead();
  26. extern void Addlist(struct addre *headnode, struct infor *list);
  27.  
  28. #endif

代码可直接复制使用,如有不足请提出,后续修改谢谢

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号