鱼C论坛

 找回密码
 立即注册
查看: 1040|回复: 2

[已解决]单链表删除出错

[复制链接]
发表于 2020-5-23 23:16:41 | 显示全部楼层 |阅读模式

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

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

x
求教,这个程序在删除头结点的时候会出错 请求大佬支援,指出错误,十分感谢!代码格式有问题也希望大佬指出,会努力改进的。
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. #define MAXSIZE 100     //内存池最大容量

  5. //结构体定义
  6. typedef struct student *ptrL;
  7. struct student
  8. {
  9.     char num[10];
  10.     char name[10];
  11.     float score[3];
  12.     ptrL next;
  13. };

  14. //函数声明
  15. void print(ptrL p);
  16. ptrL addStu(ptrL p);
  17. ptrL findStu(ptrL p);
  18. void change(ptrL p);
  19. ptrL deleteStu(ptrL p);
  20. void display(ptrL p);
  21. void exitlist(ptrL p);

  22. //内存池
  23. ptrL pool = NULL;
  24. int count = 0;

  25. //输出学生信息
  26. void print(ptrL p)
  27. {
  28.     if (p == NULL)
  29.         return;
  30.     printf("该学生信息为:\n学号:%s\n姓名:%s\n", p->num, p->name);
  31.     for (int i = 0; i < 3; i++)
  32.     {
  33.         if (i == 0)
  34.             printf("语文");
  35.         else if (i == 1)
  36.             printf("数学");
  37.         else
  38.             printf("英语");
  39.         printf("成绩为:%.2f\n", p->score[i]);
  40.     }
  41.     return;
  42. }

  43. //插入函数
  44. ptrL addStu(ptrL p)
  45. {
  46.     ptrL p1, s = p;
  47.     if (pool != NULL)
  48.     {
  49.         p1 = pool;
  50.         pool = pool->next;
  51.         count--;
  52.     }
  53.     else
  54.     {
  55.         p1 = (ptrL)malloc(sizeof(struct student));
  56.         if (p1 == NULL)
  57.         {
  58.             printf("申请失败!\n");
  59.             return NULL;
  60.         }
  61.     }
  62.     p1->next = NULL;

  63.     printf("******请输入学生信息******\n请输入学生学号:");
  64.     scanf("%s", p1->num);
  65.     printf("请输入学生姓名:");
  66.     scanf("%s", p1->name);
  67.     for (int i = 0; i < 3; i++)
  68.     {
  69.         if (i == 0)
  70.             printf("请输入学生语文成绩:");
  71.         else if (i == 1)
  72.             printf("请输入学生数学成绩:");
  73.         else
  74.             printf("请输入学生英语成绩:");
  75.         scanf("%f", &p1->score[i]);
  76.     }
  77.     if (p == NULL)
  78.     {
  79.         p = p1;
  80.         printf("创建第一个学生成功!\n");
  81.         return p;
  82.     }

  83.     while (s->next != NULL)
  84.     {
  85.         s = s->next;
  86.     }
  87.     s->next = p1;
  88.     printf("插入成功!\n");
  89.     return p;
  90. }

  91. //查找函数(按学号查找)返回学生信息地址
  92. ptrL findStu(ptrL p)
  93. {
  94.     char num[10];
  95.     printf("请输入学生学号:");
  96.     scanf("%s", num);
  97.     while (strcmp(num, p->num) != 0 && p->next != NULL)
  98.     {
  99.         p = p->next;
  100.     }
  101.     if (p->next == NULL && strcmp(num, p->num) != 0)
  102.     {
  103.         printf("学号输入错误,未查询到相关信息!\n");
  104.         return NULL;
  105.     }
  106.     return p;
  107. }

  108. //更改学生信息
  109. void change(ptrL p)
  110. {
  111.     ptrL p1;
  112.     int a;

  113.     p1 = findStu(p);
  114.     if (p1 == NULL)
  115.     {
  116.         printf("更改失败!\n");
  117.         return;
  118.     }
  119.     print(p1);

  120.     printf("请选择需要更改的学生信息(1、学号 2、姓名 3、语文成绩 4、数学成绩 5、英语成绩):");
  121.     scanf("%d", &a);
  122.     printf("请输入替换信息:");
  123.     if (a == 1)
  124.         scanf("%s", p1->num);
  125.     else if (a == 2)
  126.         scanf("%s", p1->name);
  127.     else
  128.         scanf("%f", &p1->score[a - 3]);
  129.     printf("更改信息成功!\n");
  130.     return;
  131. }

  132. //删除学生信息
  133. ptrL deleteStu(ptrL p)
  134. {
  135.     ptrL s = p, p1;

  136.     p1 = findStu(p);
  137.     if (p1 == NULL)
  138.     {
  139.         printf("删除失败!\n");
  140.         return p;
  141.     }
  142.     print(p1);

  143.     if (p1 == p)
  144.     {
  145.         p = p->next;
  146.         if (count <= MAXSIZE)
  147.         {
  148.             ptrL temp = pool;
  149.             pool = p1;
  150.             pool->next = temp;
  151.             count++;
  152.         }
  153.         else
  154.             free(p1);
  155.         printf("删除成功!\n");
  156.         return p;
  157.     }
  158.     while (s->next != p1)
  159.     {
  160.         s = s->next;
  161.     }
  162.     s->next = p1->next;
  163.     if (count <= MAXSIZE)
  164.     {
  165.         ptrL temp = pool;
  166.         pool = p1;
  167.         pool->next = temp;
  168.         count++;
  169.     }
  170.     else
  171.         free(p1);
  172.     printf("删除成功!\n");
  173.     return p;
  174. }

  175. //显示当前学生表信息
  176. void display(ptrL p)
  177. {
  178.     if (p == NULL)
  179.     {
  180.         printf("列表为空,输出失败!\n");
  181.         return;
  182.     }
  183.     int i = 1;
  184.     while (p != NULL)
  185.     {
  186.         printf("第 %d 位学生信息:\n学号:%s\n姓名:%s\n", i, p->num, p->name);
  187.         for (int j = 0; j < 3; j++)
  188.         {
  189.             if (j == 0)
  190.                 printf("语文");
  191.             else if (j == 1)
  192.                 printf("数学");
  193.             else
  194.                 printf("英语");
  195.             printf("成绩:%.2f\n", p->score[j]);
  196.         }
  197.         p = p->next;
  198.         i++;
  199.         printf("\n");
  200.     }
  201.     return;
  202. }

  203. //退出
  204. void exitlist(ptrL p)
  205. {
  206.     ptrL s;
  207.     while (p != NULL)
  208.     {
  209.         s = p;
  210.         p = p->next;
  211.         free(s);
  212.     }
  213.     while (pool != NULL)
  214.     {
  215.         s = pool;
  216.         pool = pool->next;
  217.         free(s);
  218.     }
  219.     printf("程序结束!\n");
  220.     exit(0);
  221. }

  222. int main(void)
  223. {
  224.     int a;
  225.     ptrL p = NULL;
  226.     printf("**************学生信息表**************\n");
  227.     while (1)
  228.     {
  229.         printf("请选择你需要进行的操作(1、添加学生信息 2、查找学生信息 3、更改学生信息 4、删除学生信息 5、显示当前列表 6、退出):");
  230.         scanf("%d", &a);

  231.         if (a == 1)
  232.             p = addStu(p);
  233.         else if (a == 2)
  234.         {
  235.             ptrL s;
  236.             s = findStu(p);
  237.             print(s);
  238.         }
  239.         else if (a == 3)
  240.             change(p);
  241.         else if (a == 4)
  242.             deleteStu(p);
  243.         else if (a == 5)
  244.             display(p);
  245.         else if (a == 6)
  246.             exitlist(p);
  247.         else
  248.             printf("输入错误!\n");
  249.         printf("\n");
  250.     }
  251.     return 0;
  252. }
复制代码
最佳答案
2020-5-24 02:24:19
设断点,单步调试。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-24 02:24:19 | 显示全部楼层    本楼为最佳答案   
设断点,单步调试。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-5-24 10:20:22 | 显示全部楼层
赚小钱 发表于 2020-5-24 02:24
设断点,单步调试。

感谢大佬指点,问题已解决。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-7 11:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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