鱼C论坛

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

[已解决]尾插法,tail_add寻找尾节点显示访问异常,67行

[复制链接]
发表于 2021-1-2 14:17:00 | 显示全部楼层 |阅读模式

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

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

x
  1. #include<winuser.inl>
  2. #include<stdio.h>
  3. #include <cstdlib>
  4. #include<string.h>
  5. //输入学生的总人数以及每个学生的姓名成绩,并将内容打印出来
  6. //用尾巴插法向链表里面添加元素
  7. void getInput(struct Student* stu);
  8. void printfInfo(struct Student** head);
  9. void releaseMemory(struct Student** head);
  10. void tail_add(struct Student** head);

  11. struct Student
  12. {
  13.         char name[20];
  14.         int score;
  15.         struct Student* next;
  16. };

  17. void getInput(struct Student* stu)
  18. {
  19.         printf("name:");
  20.         scanf_s("%s", stu->name, 20);
  21.         printf("score:");
  22.         scanf_s("%d", &stu->score);
  23. }

  24. void printfInfo(struct Student** head)
  25. {
  26.         struct Student* stu;
  27.         stu = *head;
  28.         while (stu != NULL)
  29.         {
  30.                 printf("name: %s \n", stu->name);
  31.                 printf("score:%d \n", stu->score);
  32.                 stu = stu->next;
  33.         }
  34. }

  35. void releaseMemory(struct Student** head)
  36. {
  37.         struct Student* stu, * temp;
  38.         stu = *head;
  39.         while (stu != NULL)
  40.         {
  41.                 temp = stu;
  42.                 stu = stu->next;
  43.                 free(temp);
  44.         }
  45. }

  46. void tail_add(struct Student** head)
  47. {
  48.         struct Student* stu, * temp;

  49.         stu = (struct Student*)(malloc(sizeof(struct Student)));
  50.         if (stu == NULL)
  51.         {
  52.                 printf("memory failed");
  53.                 exit(1);
  54.         }
  55.         getInput(stu);

  56.         if (*head != NULL)
  57.         {
  58.                 temp = *head;
  59.                 //找尾节点
  60.                 while (temp->next != NULL)
  61.                 {
  62.                         temp = temp->next;
  63.                 };

  64.                 //插入数据
  65.                 temp = stu;
  66.                 stu->next = NULL;
  67.         }

  68.         else
  69.         {
  70.                 *head = stu;
  71.                 stu->next = NULL;
  72.         }
  73. }

  74. int main()
  75. {
  76.         struct Student* namelist = NULL;  ////初始化一个指针,头指针,相当于定义了一个空的单链表
  77.         struct Student* stu = NULL;

  78.         int num;
  79.         printf("total num of stu:");
  80.         scanf_s("%d", &num);
  81.         printf("\n");

  82.         printf("enter info:\n");
  83.         for (int i = 0; i < num; i++)
  84.         {
  85.                 tail_add(&stu);
  86.         }
  87.         printf("finish entering!\n");

  88.         printf("begining to print\n");
  89.         printfInfo(&stu);

  90.         releaseMemory(&namelist);
  91.         return 0;

  92. }
复制代码
最佳答案
2021-1-2 14:37:36
       第 73 行
  1.                 temp = stu ;
复制代码

       改为
  1.                 temp -> next = stu ;
复制代码
捕获.PNG
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-2 14:37:36 | 显示全部楼层    本楼为最佳答案   
       第 73 行
  1.                 temp = stu ;
复制代码

       改为
  1.                 temp -> next = stu ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 16:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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