鱼C论坛

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

[已解决]单链表头插法,打印报错printfInfo函数

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

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

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

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 head_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 head_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.                 *head = stu;
  60.                 stu->next = temp;
  61.         }
  62.         else
  63.         {
  64.                 *head = stu;
  65.                 stu->next = NULL;
  66.         }
  67. }

  68. int main()
  69. {
  70.         struct Student *namelist = NULL;  ////初始化一个指针,头指针,相当于定义了一个空的单链表
  71.         struct Student* stu;
  72.        
  73.         int num;
  74.         printf("total num of stu:");
  75.         scanf_s("%d", &num);
  76.         printf("\n");

  77.         printf("enter info:\n");
  78.         for (int i = 0; i < num; i++)
  79.         {
  80.                 head_add(&stu);
  81.         }
  82.         printf("finish entering!\n");

  83.         printf("begining to print\n");
  84.         printfInfo(&stu);

  85.         releaseMemory(&namelist);
  86.         return 0;

  87. }
复制代码
最佳答案
2021-1-2 12:49:57
  1. int main()
  2. {
  3.         struct Student *namelist = NULL;  ////初始化一个指针,头指针,相当于定义了一个空的单链表
  4.         struct Student* stu;              //  指针 stu 未初始化
复制代码

        初始化一下就好了
  1.         struct Student* stu = NULL ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-1-2 12:49:57 | 显示全部楼层    本楼为最佳答案   
  1. int main()
  2. {
  3.         struct Student *namelist = NULL;  ////初始化一个指针,头指针,相当于定义了一个空的单链表
  4.         struct Student* stu;              //  指针 stu 未初始化
复制代码

        初始化一下就好了
  1.         struct Student* stu = NULL ;
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-5-3 17:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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