鱼C论坛

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

[已解决]链表程序报错

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

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

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

x
本帖最后由 水吉雨文 于 2021-12-2 16:42 编辑

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

struct student
{
    long num;
    float score;
};

struct node
{
    struct student data;
    struct node *next;
};

//创建链表
struct node *creatlist()
{
    struct node *headnode=(struct node *)malloc(sizeof(struct node));
    headnode->next=NULL;
    return headnode;
}

//创建结点
struct node *creatnode(struct node data)
{
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
}

//打印结点
void printlist(struct node *headnode)
{
    struct node *pmove=headnode->next;
    while(pmove!=NULL)
    {
        printf("%d %d\n",pmove->data.num,pmove->data.score);
        pmove=pmove->next;
    }
}

//插入结点
void insertnode(struct node *headnode,struct node data)
{
    struct node *newnode=creatnode(data);//创建结点
    newnode->next=headnode->next;
    headnode->next=newnode;
}

int main()
{
    struct student info;
    struct node *list=creatlist();
    for(int i=1;i<=3;i++)
    {
        printf("请输入学生的学号和成绩");
        scanf("%d %d",&info.num,&info.score);
        insertnode(list,info);
    }
    printlist(list);
    return 0;
}


编译器一直提示创建结点函数出问题了
怎么改啊,求大佬们帮小白看看怎么做
最佳答案
2021-12-2 17:02:22
本帖最后由 jhq999 于 2021-12-2 20:57 编辑
#include <stdio.h>
#include <stdlib.h>

struct student
{
    long num;
    float score;
};

struct node
{
    struct student data;
    struct node *next;
};

//创建链表
struct node *creatlist()
{
    struct node *headnode=(struct node *)malloc(sizeof(struct node));
    headnode->next=NULL;
    return headnode;
}

//创建结点
struct node *creatnode(struct student data)//////////////////////////
{
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
}

//打印结点
void printlist(struct node *headnode)
{
    struct node *pmove=headnode->next;
    while(pmove!=NULL)
    {
        printf("%d %d\n",pmove->data.num,pmove->data.score);
        pmove=pmove->next;
    }
}

//插入结点
void insertnode(struct node *headnode,struct student data)//////////////////////
{
    struct node *newnode=creatnode(data);//创建结点
    newnode->next=headnode->next;
    headnode->next=newnode;
}

int main()
{
    struct student info;
    struct node *list=creatlist();
    for(int i=1;i<=3;i++)
    {
        printf("请输入学生的学号和成绩");
        scanf("%d %d",&info.num,&info.score);
        insertnode(list,info);
    }
    printlist(list);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-12-2 17:02:22 | 显示全部楼层    本楼为最佳答案   
本帖最后由 jhq999 于 2021-12-2 20:57 编辑
#include <stdio.h>
#include <stdlib.h>

struct student
{
    long num;
    float score;
};

struct node
{
    struct student data;
    struct node *next;
};

//创建链表
struct node *creatlist()
{
    struct node *headnode=(struct node *)malloc(sizeof(struct node));
    headnode->next=NULL;
    return headnode;
}

//创建结点
struct node *creatnode(struct student data)//////////////////////////
{
    struct node *newnode=(struct node *)malloc(sizeof(struct node));
    newnode->data=data;
    newnode->next=NULL;
    return newnode;
}

//打印结点
void printlist(struct node *headnode)
{
    struct node *pmove=headnode->next;
    while(pmove!=NULL)
    {
        printf("%d %d\n",pmove->data.num,pmove->data.score);
        pmove=pmove->next;
    }
}

//插入结点
void insertnode(struct node *headnode,struct student data)//////////////////////
{
    struct node *newnode=creatnode(data);//创建结点
    newnode->next=headnode->next;
    headnode->next=newnode;
}

int main()
{
    struct student info;
    struct node *list=creatlist();
    for(int i=1;i<=3;i++)
    {
        printf("请输入学生的学号和成绩");
        scanf("%d %d",&info.num,&info.score);
        insertnode(list,info);
    }
    printlist(list);
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-23 05:21

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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