鱼C论坛

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

[已解决]新手求助,想请问一个结构体的链表问题,望解答

[复制链接]
发表于 2020-4-24 16:19:21 | 显示全部楼层 |阅读模式

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

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

x
本帖最后由 光着屁股的犀牛 于 2020-4-24 16:28 编辑

想请问一个结构体的链表的小问题,望解答
为啥我这个新建的结构体变量会报错
local variable 'ne' used without having been initialized
未初始化就使用了局部变量“ne”
[b]问题出错在第   92,  92,  92    行,代码可能有点长,辛苦,辛苦,只是一个新建变量的问题出错,应该是个小问题,但是自己解决不了,望求助;[/b]

                               
登录/注册后可看大图

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define len sizeof(struct student)

  4. int n = 0;

  5. struct student
  6. {
  7.         int num;
  8.         char name[20];
  9.         int score;
  10.         struct student *next;
  11. };
  12. struct student *head;

  13. int main ()
  14. {
  15.         struct student *creat(void);
  16.         void print(struct student *a);
  17.         struct student *delet(struct student *a);
  18.         struct student *inc(struct student *a);
  19.         int n;
  20.         creat();
  21.         print(head);
  22.         printf ("what do you want to do:\ndelete enter :1\tincreat enter :2");
  23.         scanf ("%d",&n);
  24.         if(n == 1)
  25.         {
  26.                 print(delet(head));
  27.         }
  28.         else if(n == 2)
  29.         {
  30.                 print(inc(head));
  31.         }
  32. }

  33. struct student *creat(void)
  34. {
  35.         struct student *p1,*p2;
  36.         p1 = p2 = (struct student *)malloc(len);
  37.         printf ("please enter the student's number:");
  38.         scanf ("%d",&p1->num);
  39.         printf ("please enter the student's name  :");
  40.         scanf ("%s",p1->name);
  41.         printf ("please enter the student's score :");
  42.         scanf ("%d",&p1->score);
  43.         printf ("\n");
  44.         head = NULL;
  45.         while (p1->num)
  46.         {
  47.                 n++;
  48.                 if(n == 1)
  49.                 {
  50.                         head = p1;
  51.                 }
  52.                 else
  53.                 {
  54.                         p2->next = p1;
  55.                 }
  56.                 p2 = p1;
  57.                 p1 = (struct student *)malloc(len);
  58.                 printf ("please enter the student's number:");
  59.                 scanf ("%d",&p1->num);
  60.                 printf ("please enter the student's name  :");
  61.                 scanf ("%s",p1->name);
  62.                 printf ("please enter the student's score :");
  63.                 scanf ("%d",&p1->score);
  64.                 printf ("\n");
  65.         }
  66.         p2->next =NULL;
  67.         return head;
  68. }

  69. void print(struct student *a)
  70. {
  71.         struct student *p;
  72.         p = a;
  73.         while (p)
  74.         {
  75.                 printf("\nnumber:%d\tname:%s\tscore:%d\t",p->num,p->name,p->score);
  76.                 p = p->next;
  77.         }
  78. }

  79. struct student *inc(struct student *a)
  80. {
  81.         struct student *p1,*p0,*p2;
  82.         struct student *ne;
  83.         int loc;
  84.         printf ("enter the number you want to increase:\n");
  85.         printf ("please enter the student's number:");
  86.         scanf ("%d",&ne->num);                                                                                //问题出处!问题出处!问题出处!问题出处!问题出处!
  87.         printf ("please enter the student's name  :");
  88.         scanf ("%s",ne->name);
  89.         printf ("please enter the student's score :");
  90.         scanf ("%d",&ne->score);
  91.         printf ("\n");
  92.         printf ("\nplease enter the location you want to add:");
  93.         scanf ("%d",&loc);
  94.         p1 = a;
  95.         p0 = ne;
  96.         if (a == NULL)
  97.         {
  98.                 return ne;
  99.         }
  100.         else
  101.         {
  102.                 while (p0->num>p1->num && p1->next!= NULL)
  103.                 {
  104.                         p2 = p1;
  105.                         p1 = p1->next;
  106.                 }
  107.                 if (p0->num<=p1->num)
  108.                 {
  109.                         if(p1 == head)
  110.                         {
  111.                                 head = p0;
  112.                                 p0->next = p1;
  113.                         }
  114.                         else
  115.                         {
  116.                                 p2->next = p0;
  117.                                 p0->next = p1;
  118.                         }
  119.                 }
  120.                 else
  121.                 {
  122.                         p1->next = p0;
  123.                         p0->next = NULL;
  124.                 }
  125.         }
  126.         n = n+1;
  127.         return a;
  128. }



  129. struct student *delet(struct student *a)
  130. {
  131.         struct student *p1,*p2;
  132.         int num;
  133.         printf ("输入你要找的数:");
  134.         scanf ("%d",&num);
  135.         if (a == NULL)
  136.         {
  137.                 printf ("空表");
  138.                 goto END;
  139.         }
  140.         p1 = a;
  141.         while (num != p1->num && p1->next != NULL)
  142.         {
  143.                 p2 = p1;
  144.                 p1 = p1->next;
  145.         }
  146.         if (num == p1->num)
  147.         {
  148.                 if(p1 == head)
  149.                 {
  150.                         head = p1->next;
  151.                 }
  152.                 else
  153.                 {
  154.                         p2->next = p1->next;
  155.                 }
  156.         }
  157.         else
  158.         {
  159.                 printf("找不到");
  160.         }
  161.         printf("成功");
  162.         n--;
  163. END:
  164.         return a;
  165. }
复制代码

希望有高手能帮忙指点一下,谢谢
最佳答案
2020-4-24 16:53:07
你只是定义了一个该结构体的指针啊。
指针没有指向,当然会报错说没有初始化啦!
将该指针指向一个该结构体的对象,这叫指针的初始化。
指针不进行初始化,是不能用的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-4-24 16:53:07 | 显示全部楼层    本楼为最佳答案   
你只是定义了一个该结构体的指针啊。
指针没有指向,当然会报错说没有初始化啦!
将该指针指向一个该结构体的对象,这叫指针的初始化。
指针不进行初始化,是不能用的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-24 17:20:30 | 显示全部楼层
sunrise085 发表于 2020-4-24 16:53
你只是定义了一个该结构体的指针啊。
指针没有指向,当然会报错说没有初始化啦!
将该指针指向一个该结构 ...

感谢感谢,万分感谢,还是自己基础不扎实
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-7-12 23:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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