鱼C论坛

 找回密码
 立即注册
查看: 1627|回复: 6

[已解决]【C语言】通讯录系统设计中遇到的问题

[复制链接]
发表于 2020-9-6 17:53:10 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
各位大佬好!我用C语言编写了个通讯录系统,系统的要求就是小甲鱼视频当中的要求(见下图)。目前在删除通讯录这边碰到了bug。删除通讯录当中的问题就是当我执行删除操作后,整个主函数会直接结束,不知道为什么,还请各位大佬麻烦看看。

小甲鱼的要求:
截屏2020-09-06 下午5.43.56.png

全部代码:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. struct Address_book;

  5. void addPreson(struct Address_book **);

  6. void findPerson(struct Address_book *);

  7. void changePerson(struct Address_book *);

  8. void delPerson(struct Address_book **);

  9. void displayContacts(struct Address_book *);

  10. void freeContacts(struct Address_book *);

  11. int main(void) {
  12.     int input;
  13.     char name[40];
  14.     long long phone_number;
  15.     struct Address_book *head = NULL;//头指针

  16.     do {
  17.         printf("请输入操作号码:\n"
  18.                "1.插入新的联系人\t2.查找已有联系人\t3.更改已有联系人\n"
  19.                "4.删除已有联系人\t5.显示当前通讯录\t6.退出通讯录");
  20.         putchar('\n');
  21.         scanf("%d", &input);
  22.         if (input < 1 || input > 6) {
  23.             printf("非法输入!");
  24.             continue;
  25.         }


  26.         switch (input) {
  27.             case 1 :
  28.                 addPreson(&head);
  29.                 break;
  30.             case 2:
  31.                 findPerson(head);
  32.                 break;
  33.             case 3:
  34.                 changePerson(head);
  35.                 break;
  36.             case 4:
  37.                 delPerson(&head);
  38.                 break;
  39.             case 5:
  40.                 displayContacts(head);
  41.                 break;
  42.             case 6:
  43.                 freeContacts();
  44.                 break;
  45.         }
  46.     } while (input != 6);

  47.     return 0;
  48. }

  49. struct Address_book {
  50.     char name[40];
  51.     long long phone_number;
  52.     struct Address_book *next;
  53. };

  54. void addPreson(struct Address_book **head) {    //添加
  55.     struct Address_book *previous;
  56.     struct Address_book *new;

  57.     new = (struct Address_book *) malloc(sizeof(struct Address_book));
  58.     if (new == NULL) {
  59.         printf("分配新空间失败,请重试!");
  60.         exit(1);
  61.     }

  62.     //给new赋值
  63.     printf("请输入添加的联系人姓名:");
  64.     scanf("%s", new->name);
  65.     printf("请输入添加的联系人电话:");
  66.     scanf("%lld", &new->phone_number);

  67.     if (*head != NULL) {
  68.         previous = *head;
  69.         //定义单链表尾部位置
  70.         while (previous->next != NULL) {
  71.             previous = previous->next;
  72.         }
  73.         //插入数据
  74.         previous->next = new;
  75.         new->next = NULL;
  76.     } else {
  77.         *head = new;
  78.         new->next = NULL;
  79.     }
  80. }

  81. void findPerson(struct Address_book *head) {    //查找
  82.     char name[40];
  83.     printf("请输入你想查找的联系人姓名:");
  84.     scanf("%s", name);

  85.     struct Address_book *object;
  86.     object = NULL;
  87.     while (head != NULL) {
  88.         if (!strcmp(head->name, name)) {
  89.             object = head;
  90.             printf("查询完毕!\n");
  91.             printf("联系人姓名:%s\n", name);
  92.             printf("联系人电话%lld\n", object->phone_number);
  93.             return;
  94.         }
  95.         head = head->next;
  96.     }

  97.     printf("未找到该联系人!\n");
  98. }

  99. void changePerson(struct Address_book *head) {  //替换
  100.     char name_1[40];
  101.     printf("请输入替换人的姓名:");
  102.     scanf("%s", name_1);

  103.     while (head != NULL) {
  104.         if (!strcmp(name_1, head->name)) {
  105.             printf("请输入替换后的姓名:");
  106.             scanf("%s", head->name);
  107.             printf("请输入替换后的电话:");
  108.             scanf("%lld", &head->phone_number);
  109.             printf("替换成功!\n");
  110.             return;
  111.         }
  112.         head = head->next;
  113.     }
  114.     printf("未找到该联系人!\n");
  115. }

  116. void delPerson(struct Address_book **head) { //删除指令
  117.     char name[40];
  118.     printf("请输入要删除的联系人姓名:");
  119.     scanf("%s", name);

  120.     struct Address_book *previous = NULL;  //前一个指针
  121.     struct Address_book *current = *head;   //当前要删除的指针

  122.     while ((*head) != NULL) {
  123.         if (!strcmp(current->name, name)) { //定位完毕,删除,结束
  124.             previous->next = current->next;
  125.             free(current);
  126.             return;
  127.         }
  128.         previous = current;
  129.         current = current->next;
  130.     }

  131.     printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
  132. }


  133. void displayContacts(struct Address_book *head) {
  134.     struct Address_book *current;
  135.     current = head;
  136.     if (current == NULL) {
  137.         printf("通讯录为空!");
  138.     }
  139.     while (current != NULL) {
  140.         printf("姓名:%s\n", current->name);
  141.         printf("电话:%lld\n", current->phone_number);
  142.         current = current->next;
  143.     }

  144.     putchar('\n');
  145. }

  146. void freeContacts(struct Address_book *head) {
  147.     struct Address_book *temp;
  148.     while (head != NULL) {
  149.         temp = head;
  150.         free(head);
  151.         head = temp->next;
  152.     }
  153. }
复制代码
最佳答案
2020-9-7 19:00:39
抱歉我说错了,删除函数改成这样

  1. void delPerson(struct Address_book **head) { //删除指令
  2.     char name[40];
  3.     printf("请输入要删除的联系人姓名:");
  4.     scanf("%s", name);

  5.     struct Address_book *previous = *head;  //前一个指针
  6.     struct Address_book *current = *head;   //当前要删除的指针

  7.     while ((*head) != NULL) {
  8.         if (!strcmp(current->name, name)) { //定位完毕,删除,结束
  9.             previous->next = current->next;
  10.             free(current);
  11.             return;
  12.         }
  13.         current = current->next;
  14.     }

  15.     printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
  16. }
复制代码

另外你的56行释放函数也要改一下
  1. freeContacts(head); // 不放参数?
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-9-6 17:54:35 | 显示全部楼层
各位大佬不好意思,之前发帖一直碰到问题,所以只能把全部代码一次性发出来了,还麻烦各位大佬找到删除函数了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-6 19:41:52 | 显示全部楼层
代码太长了,我就说哪个地方改吧
144行初始化有误,改为
  1. struct Address_book *previous = *head;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-7 19:00:39 | 显示全部楼层    本楼为最佳答案   
抱歉我说错了,删除函数改成这样

  1. void delPerson(struct Address_book **head) { //删除指令
  2.     char name[40];
  3.     printf("请输入要删除的联系人姓名:");
  4.     scanf("%s", name);

  5.     struct Address_book *previous = *head;  //前一个指针
  6.     struct Address_book *current = *head;   //当前要删除的指针

  7.     while ((*head) != NULL) {
  8.         if (!strcmp(current->name, name)) { //定位完毕,删除,结束
  9.             previous->next = current->next;
  10.             free(current);
  11.             return;
  12.         }
  13.         current = current->next;
  14.     }

  15.     printf("未找到该联系人!\n");//函数走到了最后,说明没能遍历到
  16. }
复制代码

另外你的56行释放函数也要改一下
  1. freeContacts(head); // 不放参数?
复制代码

评分

参与人数 1荣誉 +5 鱼币 +5 贡献 +3 收起 理由
z2521367787 + 5 + 5 + 3 无条件支持楼主!

查看全部评分

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-7 19:01:28 | 显示全部楼层
昨天我都忘了。。。可以忽略我上一个答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-9-9 06:19:40 | 显示全部楼层
没有解决吗?我试的可以啊。看看这个帖子
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-9-13 09:48:41 | 显示全部楼层
巴巴鲁 发表于 2020-9-7 19:00
抱歉我说错了,删除函数改成这样
另外你的56行释放函数也要改一下

太感谢了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-13 12:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表