花开副富贵 2023-02-09 20:32 采纳率: 66.7%
浏览 45
已结题

C语言链表初步--学生信息录入

能请帮我看看为什么连续输入学生信息后输出的姓名都是一样的?

#include <stdio.h>
#include <stdlib.h>
struct Student
{
    char* name;
    int num;
    double height;
};
struct Node
{
    struct Student data;
    struct Node* next;
};
//创建一个链表(表头)
struct Node*createlist()
{
    struct Node* headNode=(struct Node*)malloc(sizeof(struct Node));
    headNode->next=NULL;
    return headNode;
};
//创建一个结点
struct Node*createNode(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)
    {
        printf("%s\t%d\t%lf\n",pmove->data.name,pmove->data.num,pmove->data.height);
        pmove=pmove->next;
    }
    printf("\n");
}
//头插法插入一个结点
void insertNodeByHeadNode(struct Node*headNode,struct Student data)
{
    struct Node*newNode=createNode(data);
    newNode->next=headNode->next;
    headNode->next=newNode;
}
//寻找尾结点
struct Node*endofList(struct Node*list)
{
    while(list->next) list = list->next;
    return list;
}
//尾插法插入一个结点
void insertNodeByTrall(struct Node*list,struct Student data)
{
    struct Node *lastNode = endofList(list);
    struct Node *newNode  = createNode(data);
    lastNode->next = newNode;
}
int main()
{
    struct Node*list=createlist();
    struct Student info;
    while(1)
    {
        printf("请输入学生的姓名、学号、身高:");
        scanf("%s%d%lf",info.name,&info.num,&info.height);
        setbuf(stdin,NULL);
        insertNodeByTrall(list,info);
        printf("继续输入(Y/N)?");
        int choice2=getchar();
        if(choice2=='n'||choice2=='N')break;
    }
    printList(list);
    return 0;
}
  • 写回答

3条回答 默认 最新

  • qzjhjxj 2023-02-10 09:03
    关注

    第66行前插入一行:info.name = (char*)malloc(sizeof(char) * 16);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月18日
  • 已采纳回答 2月10日
  • 创建了问题 2月9日

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度