鱼C论坛

 找回密码
 立即注册
查看: 1370|回复: 1

【C语言】通讯录系统设计中遇到的问题

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

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

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

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

小甲鱼的要求

小甲鱼的要求


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

  5.     struct Address_book *previous = NULL;  //前一个指针
  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.         previous = current;
  14.         current = current->next;
  15.     }

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


其中Address_book是我创建的结构体,用于存放联系人姓名(char类型数组)和联系人电话(long long类型):
  1. struct Address_book {
  2.     char name[40];
  3.     long long phone_number;
  4.     struct Address_book *next;
  5. };
复制代码


主函数:
  1. int main(void) {
  2.     int input;
  3.     char name[40];
  4.     long long phone_number;
  5.     struct Address_book *head = NULL;//头指针

  6.     do {
  7.         printf("请输入操作号码:\n"
  8.                "1.插入新的联系人\t2.查找已有联系人\t3.更改已有联系人\n"
  9.                "4.删除已有联系人\t5.显示当前通讯录\t6.退出通讯录");
  10.         putchar('\n');
  11.         scanf("%d", &input);
  12.         if (input < 1 || input > 6) {
  13.             printf("非法输入!");
  14.             continue;
  15.         }


  16.         switch (input) {
  17.             case 1 :
  18.                 addPreson(&head);
  19.                 break;
  20.             case 2:
  21.                 findPerson(head);
  22.                 break;
  23.             case 3:
  24.                 changePerson(head);
  25.                 break;
  26.             case 4:
  27.                 delPerson(&head);
  28.                 break;
  29.             case 5:
  30.                 displayContacts(head);
  31.                 break;
  32.             case 6:
  33.                 freeContacts();
  34.                 break;
  35.         }
  36.     } while (input != 6);

  37.     return 0;
  38. }
复制代码


最后是整个代码,为了方便看我把最主要要解决的部分放在上面了,怕有其他问题,所以就把整个代码放最后了
  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. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-9-8 21:40:33 | 显示全部楼层
本帖最后由 superbe 于 2020-9-8 21:52 编辑

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

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

  7.     while (current != NULL) {               //判断当前节点是否为NULL
  8.         if (!strcmp(current->name, name)) { //定位完毕,删除,结束
  9.             if (previous == NULL)           //头节点即是要删除的节点的情况
  10.                 *head = current->next;
  11.             else
  12.                 previous->next = current->next;
  13.             free(current);
  14.             return;
  15.         }
  16.         previous = current;
  17.         current = current->next;
  18.     }

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


另外,主函数调用freeContacts()漏掉了参数,改成:
freeContacts(head);
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 03:36

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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