水吉雨文 发表于 2021-12-2 16:31:36

链表程序报错

本帖最后由 水吉雨文 于 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;
}


编译器一直提示创建结点函数出问题了
怎么改啊,求大佬们帮小白看看怎么做

jhq999 发表于 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;
}
页: [1]
查看完整版本: 链表程序报错