鱼C论坛

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

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

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

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

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

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

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

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

#define LEN sizeof(struct cesh)

struct cesh* creat();
void print(struct cesh*head);
struct cesh* shanchu(struct cesh*head,int xh);
struct cesh* charu(struct cesh* head, struct cesh* stu2);
int i;

void main()
{
        struct cesh* stu;
        struct cesh* p;
        struct cesh stu2;
        stu = creat();
        p = stu;
        print(p);
        int xh;
        //while (i)
        //{
                printf("输入你要删除的人的学号。\n");
                scanf_s("%d", &xh);
                print(shanchu(p, xh));
        //}
                printf("输入你要插入的学号。\n");
                scanf_s("%d", &stu2.xuehao);
                printf("输入成绩。\n");
                scanf_s("%lf", &stu2.score);
                p = charu(stu, &stu2);
                print(p);
        system("pause");
}

struct cesh
{
        int xuehao;
        double score;
        struct cesh* next;
};

struct cesh* creat()
{
        struct cesh *p1,*p2,*head;
        p1 = p2 = (struct cesh*)malloc(LEN);
        printf("输入学号。\n");
        scanf_s("%d", &p1->xuehao);
        printf("输入成绩。\n");
        scanf_s("%lf", &p1->score);
        head = NULL;
        i = 0;
        while (p1->xuehao)
        {
                i++;
                if (i == 1)
                {
                        head = p1;
                }

                else
                {
                        p2->next = p1;
                }
                p2 = p1;
                p1 = (struct cesh*)malloc(LEN);
                printf("输入学号\n");
                scanf_s("%d",&p1->xuehao);
                printf("输入成绩\n");
                scanf_s("%lf",&p1->score);
        }
        p2->next = NULL;
        return head;
}

void print(struct cesh* head)
{
        struct cesh* p;
        printf("\n这里一共有 %d 个数据\n\n", i);
        p = head;
        if (head)
        {
                do
                {
                        printf("学号为 %d 的成绩为 %0.2f.\n", p->xuehao, p->score);
                        p = p->next;
                } while (p);
        }
}

struct cesh* shanchu(struct cesh* head, int xh)
{
        struct cesh* p1;
        struct cesh* p2;
        p2 = p1 = head;
        if (head == NULL)
        {
                printf("已无数据。\n");
                goto end;
        }
        p1 = head;
        while ((xh != p1->xuehao) && p1->next != NULL)
        {
                p2 = p1;
                p1 = p1->next;
        }
        if (xh == p1->xuehao)
        {
                if (p1 == head)
                {
                        head = p1->next;
                }
                else
                {
                        p2->next = p1->next;
                }
                printf("\n成功删除学号为 %d 的成绩。\n", xh);
                i = i - 1;
        }
        else//正在尝试如果没有则加入此学号//bug多多
        {
                printf("\n没有找到学号: %d。\n", xh);
                //struct cesh* stu;
                //stu = creat();
                //print(stu);
        }
end:
        return head;
}

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

        struct cesh* p0, * p1, * p2;
        p1 = head;
        p0 = stu2;
        if (head == NULL)
        {
                head = p0;
                p0->next == NULL;
        }
        else
        {
                while ((p0->xuehao > p1->xuehao) && (p1->next != NULL))
                {
                        p2 = p1;
                        p1 = p1->next;
                }
                if (p0->xuehao <= p1->xuehao)
                {
                        if (p1 == head)
                        {
                                head = p0;
                                p0->next = p1;
                        }
                        else
                        {
                                p2->next = p0;
                                p0->next = p1;
                        }
                }
                else
                {
                        p1->next = p0;
                        p0->next = NULL;
                }
        }
        i = i + 1;
        return head;
}
最佳答案
2023-8-19 11:33:05
错误1:stu2使用未定义的struct "cesh"
错误原因:在主函数中声明了结构体变量stu2,但没有定义结构体"cesh"。
解决方法:在结构体定义之前添加结构体声明。
修改代码如下:
struct cesh; // 添加结构体声明

struct cesh* stu; 
struct cesh* p;
struct cesh stu2;

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

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

修改以上三处错误后,代码应该能够正常编译运行了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

struct cesh* stu; 
struct cesh* p;
struct cesh stu2;

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

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

修改以上三处错误后,代码应该能够正常编译运行了。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

1. 错误:使用未定义的 struct "cesh"。
问题出现在函数creat中的以下代码:
#define LEN sizeof(struct cesh)
这里使用了一个未定义的结构体"cesh"。需要将其修改为正确的结构体名字"struct cesh"。

2. 错误:"stu2.xuehao"的左侧必须具有结构/联合类型。
问题出现在函数main中的以下代码:
scanf_s("%d", &stu2.xuehao);
这里应该使用结构体指针&stu2->xuehao,而不是stu2.xuehao。修改为:
scanf_s("%d", &stu2->xuehao);

3. 错误:"score"的左侧必须具有结构/联合类型。
问题出现在函数main中的以下代码:
scanf_s("%lf", &stu2.score);
这里应该使用结构体指针&stu2->score,而不是stu2.score。修改为:
scanf_s("%lf", &stu2->score);

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

使用道具 举报

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

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

修改后的代码如下:
#include <stdio.h>#include <malloc.h>#include <stdlib.h>struct cesh {    int xuehao;    double score;    struct cesh* next;};#define LEN sizeof(struct cesh)struct cesh* creat();void print(struct cesh* head);struct cesh* shanchu(struct cesh* head, int xh);struct cesh* charu(struct cesh* head, struct cesh* stu2);int i;void main() {    struct cesh* stu;    struct cesh* p;    struct cesh stu2;    stu = creat();    p = stu;    print(p);    int xh;    printf("输入你要删除的人的学号。\n");    scanf_s("%d", &xh);    print(shanchu(p, xh));    printf("输入你要插入的学号。\n");    scanf_s("%d", &stu2.xuehao);    printf("输入成绩。\n");    scanf_s("%lf", &stu2.score);    p = charu(stu, &stu2);    print(p);    system("pause");}
错误2:".xuehao"的左侧必须具有结构/联合类型

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

修改后的代码如下:
struct cesh {    int xuehao;    double score;    struct cesh* next;};...struct cesh* stu2 = (struct cesh*)malloc(sizeof(struct cesh));...scanf_s("%d", &(stu2->xuehao));...
错误3:".score"的左侧必须具有结构/联合类型

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

修改后的代码如下:
struct cesh {    int xuehao;    double score;    struct cesh* next;};...struct cesh* stu2 = (struct cesh*)malloc(sizeof(struct cesh));...scanf_s("%lf", &(stu2->score));...
以上是修改后的完整代码,根据问题描述进行了相应的修改。希望能帮到你!如果还有其他问题,请随时提问。

如果回答对你有帮助,请给我一个最佳答案!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-6 08:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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