鱼C论坛

 找回密码
 立即注册
查看: 1117|回复: 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 编辑
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. struct student
  4. {
  5.     long num;
  6.     float score;
  7. };

  8. struct node
  9. {
  10.     struct student data;
  11.     struct node *next;
  12. };

  13. //创建链表
  14. struct node *creatlist()
  15. {
  16.     struct node *headnode=(struct node *)malloc(sizeof(struct node));
  17.     headnode->next=NULL;
  18.     return headnode;
  19. }

  20. //创建结点
  21. struct node *creatnode(struct student data)//////////////////////////
  22. {
  23.     struct node *newnode=(struct node *)malloc(sizeof(struct node));
  24.     newnode->data=data;
  25.     newnode->next=NULL;
  26.     return newnode;
  27. }

  28. //打印结点
  29. void printlist(struct node *headnode)
  30. {
  31.     struct node *pmove=headnode->next;
  32.     while(pmove!=NULL)
  33.     {
  34.         printf("%d %d\n",pmove->data.num,pmove->data.score);
  35.         pmove=pmove->next;
  36.     }
  37. }

  38. //插入结点
  39. void insertnode(struct node *headnode,struct student data)//////////////////////
  40. {
  41.     struct node *newnode=creatnode(data);//创建结点
  42.     newnode->next=headnode->next;
  43.     headnode->next=newnode;
  44. }

  45. int main()
  46. {
  47.     struct student info;
  48.     struct node *list=creatlist();
  49.     for(int i=1;i<=3;i++)
  50.     {
  51.         printf("请输入学生的学号和成绩");
  52.         scanf("%d %d",&info.num,&info.score);
  53.         insertnode(list,info);
  54.     }
  55.     printlist(list);
  56.     return 0;
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

  3. struct student
  4. {
  5.     long num;
  6.     float score;
  7. };

  8. struct node
  9. {
  10.     struct student data;
  11.     struct node *next;
  12. };

  13. //创建链表
  14. struct node *creatlist()
  15. {
  16.     struct node *headnode=(struct node *)malloc(sizeof(struct node));
  17.     headnode->next=NULL;
  18.     return headnode;
  19. }

  20. //创建结点
  21. struct node *creatnode(struct student data)//////////////////////////
  22. {
  23.     struct node *newnode=(struct node *)malloc(sizeof(struct node));
  24.     newnode->data=data;
  25.     newnode->next=NULL;
  26.     return newnode;
  27. }

  28. //打印结点
  29. void printlist(struct node *headnode)
  30. {
  31.     struct node *pmove=headnode->next;
  32.     while(pmove!=NULL)
  33.     {
  34.         printf("%d %d\n",pmove->data.num,pmove->data.score);
  35.         pmove=pmove->next;
  36.     }
  37. }

  38. //插入结点
  39. void insertnode(struct node *headnode,struct student data)//////////////////////
  40. {
  41.     struct node *newnode=creatnode(data);//创建结点
  42.     newnode->next=headnode->next;
  43.     headnode->next=newnode;
  44. }

  45. int main()
  46. {
  47.     struct student info;
  48.     struct node *list=creatlist();
  49.     for(int i=1;i<=3;i++)
  50.     {
  51.         printf("请输入学生的学号和成绩");
  52.         scanf("%d %d",&info.num,&info.score);
  53.         insertnode(list,info);
  54.     }
  55.     printlist(list);
  56.     return 0;
  57. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-25 06:56

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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