鱼C论坛

 找回密码
 立即注册
查看: 1119|回复: 3

[已解决]链表插入与删除

[复制链接]
发表于 2023-8-19 11:32:22 | 显示全部楼层 |阅读模式

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

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

x
请问这三个错误怎么解决?没看懂哪出错了。

stu2”使用未定义的 struct“cesh”       
“.xuehao”的左侧必须具有结构/联合类型
“.score”的左侧必须具有结构/联合类型


  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>

  4. #define LEN sizeof(struct cesh)

  5. struct cesh* creat();
  6. void print(struct cesh*head);
  7. struct cesh* shanchu(struct cesh*head,int xh);
  8. struct cesh* charu(struct cesh* head, struct cesh* stu2);
  9. int i;

  10. void main()
  11. {
  12.         struct cesh* stu;
  13.         struct cesh* p;
  14.         struct cesh stu2;
  15.         stu = creat();
  16.         p = stu;
  17.         print(p);
  18.         int xh;
  19.         //while (i)
  20.         //{
  21.                 printf("输入你要删除的人的学号。\n");
  22.                 scanf_s("%d", &xh);
  23.                 print(shanchu(p, xh));
  24.         //}
  25.                 printf("输入你要插入的学号。\n");
  26.                 scanf_s("%d", &stu2.xuehao);
  27.                 printf("输入成绩。\n");
  28.                 scanf_s("%lf", &stu2.score);
  29.                 p = charu(stu, &stu2);
  30.                 print(p);
  31.         system("pause");
  32. }

  33. struct cesh
  34. {
  35.         int xuehao;
  36.         double score;
  37.         struct cesh* next;
  38. };

  39. struct cesh* creat()
  40. {
  41.         struct cesh *p1,*p2,*head;
  42.         p1 = p2 = (struct cesh*)malloc(LEN);
  43.         printf("输入学号。\n");
  44.         scanf_s("%d", &p1->xuehao);
  45.         printf("输入成绩。\n");
  46.         scanf_s("%lf", &p1->score);
  47.         head = NULL;
  48.         i = 0;
  49.         while (p1->xuehao)
  50.         {
  51.                 i++;
  52.                 if (i == 1)
  53.                 {
  54.                         head = p1;
  55.                 }

  56.                 else
  57.                 {
  58.                         p2->next = p1;
  59.                 }
  60.                 p2 = p1;
  61.                 p1 = (struct cesh*)malloc(LEN);
  62.                 printf("输入学号\n");
  63.                 scanf_s("%d",&p1->xuehao);
  64.                 printf("输入成绩\n");
  65.                 scanf_s("%lf",&p1->score);
  66.         }
  67.         p2->next = NULL;
  68.         return head;
  69. }

  70. void print(struct cesh* head)
  71. {
  72.         struct cesh* p;
  73.         printf("\n这里一共有 %d 个数据\n\n", i);
  74.         p = head;
  75.         if (head)
  76.         {
  77.                 do
  78.                 {
  79.                         printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
  80.                         p = p->next;
  81.                 } while (p);
  82.         }
  83. }

  84. struct cesh* shanchu(struct cesh* head, int xh)
  85. {
  86.         struct cesh* p1;
  87.         struct cesh* p2;
  88.         p2 = p1 = head;
  89.         if (head == NULL)
  90.         {
  91.                 printf("已无数据。\n");
  92.                 goto end;
  93.         }
  94.         p1 = head;
  95.         while ((xh != p1->xuehao) && p1->next != NULL)
  96.         {
  97.                 p2 = p1;
  98.                 p1 = p1->next;
  99.         }
  100.         if (xh == p1->xuehao)
  101.         {
  102.                 if (p1 == head)
  103.                 {
  104.                         head = p1->next;
  105.                 }
  106.                 else
  107.                 {
  108.                         p2->next = p1->next;
  109.                 }
  110.                 printf("\n成功删除学号为 %d 的成绩。\n", xh);
  111.                 i = i - 1;
  112.         }
  113.         else//正在尝试如果没有则加入此学号//bug多多
  114.         {
  115.                 printf("\n没有找到学号: %d。\n", xh);
  116.                 //struct cesh* stu;
  117.                 //stu = creat();
  118.                 //print(stu);
  119.         }
  120. end:
  121.         return head;
  122. }

  123. struct cesh* charu(struct cesh* head, struct cesh* stu2)//第一个参数是被插入的链表
  124.                                                                                                                 //第二个参数是待插入的结构的地址
  125. {

  126.         struct cesh* p0, * p1, * p2;
  127.         p1 = head;
  128.         p0 = stu2;
  129.         if (head == NULL)
  130.         {
  131.                 head = p0;
  132.                 p0->next == NULL;
  133.         }
  134.         else
  135.         {
  136.                 while ((p0->xuehao > p1->xuehao) && (p1->next != NULL))
  137.                 {
  138.                         p2 = p1;
  139.                         p1 = p1->next;
  140.                 }
  141.                 if (p0->xuehao <= p1->xuehao)
  142.                 {
  143.                         if (p1 == head)
  144.                         {
  145.                                 head = p0;
  146.                                 p0->next = p1;
  147.                         }
  148.                         else
  149.                         {
  150.                                 p2->next = p0;
  151.                                 p0->next = p1;
  152.                         }
  153.                 }
  154.                 else
  155.                 {
  156.                         p1->next = p0;
  157.                         p0->next = NULL;
  158.                 }
  159.         }
  160.         i = i + 1;
  161.         return head;
  162. }
复制代码
最佳答案
2023-8-19 11:33:05
错误1:stu2使用未定义的struct "cesh"
错误原因:在主函数中声明了结构体变量stu2,但没有定义结构体"cesh"。
解决方法:在结构体定义之前添加结构体声明。
修改代码如下:
  1. struct cesh; // 添加结构体声明

  2. struct cesh* stu;
  3. struct cesh* p;
  4. struct cesh stu2;
复制代码


错误2:".xuehao"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".xuehao"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".xuehao"修改为"->xuehao"。
修改代码如下:
  1. if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码


错误3:".score"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".score"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".score"修改为"->score"。
修改代码如下:
  1. if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码


修改以上三处错误后,代码应该能够正常编译运行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-8-19 11:33:05 | 显示全部楼层    本楼为最佳答案   
错误1:stu2使用未定义的struct "cesh"
错误原因:在主函数中声明了结构体变量stu2,但没有定义结构体"cesh"。
解决方法:在结构体定义之前添加结构体声明。
修改代码如下:
  1. struct cesh; // 添加结构体声明

  2. struct cesh* stu;
  3. struct cesh* p;
  4. struct cesh stu2;
复制代码


错误2:".xuehao"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".xuehao"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".xuehao"修改为"->xuehao"。
修改代码如下:
  1. if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码


错误3:".score"的左侧必须具有结构/联合类型
错误原因:在插入函数charu中,使用了".score"访问结构体变量stu2的成员,但stu2是一个指向结构体的指针,需要使用"->"来访问成员。
解决方法:将".score"修改为"->score"。
修改代码如下:
  1. if (p0->xuehao <= p1->xuehao) // 修改为 p0->xuehao
复制代码


修改以上三处错误后,代码应该能够正常编译运行了。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-19 11:33:09 | 显示全部楼层
这段代码存在三个错误:

1. 错误:使用未定义的 struct "cesh"。
问题出现在函数creat中的以下代码:
  1. #define LEN sizeof(struct cesh)
复制代码

这里使用了一个未定义的结构体"cesh"。需要将其修改为正确的结构体名字"struct cesh"。

2. 错误:"stu2.xuehao"的左侧必须具有结构/联合类型。
问题出现在函数main中的以下代码:
  1. scanf_s("%d", &stu2.xuehao);
复制代码

这里应该使用结构体指针&stu2->xuehao,而不是stu2.xuehao。修改为:
  1. scanf_s("%d", &stu2->xuehao);
复制代码


3. 错误:"score"的左侧必须具有结构/联合类型。
问题出现在函数main中的以下代码:
  1. scanf_s("%lf", &stu2.score);
复制代码

这里应该使用结构体指针&stu2->score,而不是stu2.score。修改为:
  1. scanf_s("%lf", &stu2->score);
复制代码


请根据上述修改建议对代码进行相应的修正。另外,还需要注意代码中可能存在其他潜在的问题。
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-19 11:33:46 | 显示全部楼层
错误1:"stu2"使用未定义的结构"struct cesh"

这个错误是由于在使用结构体变量stu2时,没有提前定义所属的结构体struct cesh。要解决这个错误,需要在使用stu2之前先定义结构体struct cesh。可以将结构体的定义放在头文件中,或者将结构体的定义放在main()函数之前。

修改后的代码如下:
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. struct cesh {
  5.     int xuehao;
  6.     double score;
  7.     struct cesh* next;
  8. };
  9. #define LEN sizeof(struct cesh)
  10. struct cesh* creat();
  11. void print(struct cesh* head);
  12. struct cesh* shanchu(struct cesh* head, int xh);
  13. struct cesh* charu(struct cesh* head, struct cesh* stu2);
  14. int i;
  15. void main() {
  16.     struct cesh* stu;
  17.     struct cesh* p;
  18.     struct cesh stu2;
  19.     stu = creat();
  20.     p = stu;
  21.     print(p);
  22.     int xh;
  23.     printf("输入你要删除的人的学号。\n");
  24.     scanf_s("%d", &xh);
  25.     print(shanchu(p, xh));
  26.     printf("输入你要插入的学号。\n");
  27.     scanf_s("%d", &stu2.xuehao);
  28.     printf("输入成绩。\n");
  29.     scanf_s("%lf", &stu2.score);
  30.     p = charu(stu, &stu2);
  31.     print(p);
  32.     system("pause");
  33. }
复制代码

错误2:".xuehao"的左侧必须具有结构/联合类型

这个错误是由于在赋值操作中,左侧的变量不是结构体类型或联合体类型。要解决这个错误,可以将结构体变量定义为指针类型,然后使用动态内存分配函数malloc()为其分配内存空间。

修改后的代码如下:
  1. struct cesh {
  2.     int xuehao;
  3.     double score;
  4.     struct cesh* next;
  5. };
  6. ...
  7. struct cesh* stu2 = (struct cesh*)malloc(sizeof(struct cesh));
  8. ...
  9. scanf_s("%d", &(stu2->xuehao));
  10. ...
复制代码

错误3:".score"的左侧必须具有结构/联合类型

这个错误与错误2类似,也是赋值操作中左侧变量的类型不匹配。同样地,需要将结构体变量定义为指针类型,并使用动态内存分配函数malloc()为其分配内存空间。

修改后的代码如下:
  1. struct cesh {
  2.     int xuehao;
  3.     double score;
  4.     struct cesh* next;
  5. };
  6. ...
  7. struct cesh* stu2 = (struct cesh*)malloc(sizeof(struct cesh));
  8. ...
  9. scanf_s("%lf", &(stu2->score));
  10. ...
复制代码

以上是修改后的完整代码,根据问题描述进行了相应的修改。希望能帮到你!如果还有其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-22 09:42

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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