写一个程序,定义以下结构类型,调用函数创建一个动态链表,并且输出该链表。 struct Student { int num; char name[20]; float score[3]; struct Student * next; }; 测试用例:程序执

以下是创建动态链表并输出该链表的 C 程序:

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

// 定义学生结构体 struct Student { int num; char name[20]; float score[3]; struct Student *next; };

int main(void) { struct Student *head = NULL; // 定义头指针 struct Student *p, *q; // p 指向新节点,q 指向尾节点 int num; char name[20]; float score[3];

// 读入学生信息,当 num 为 0 时停止读入 printf("请输入学生信息(格式:num, name, score1, score2, score3):\n"); while (1) { scanf("%d, %s, %f, %f, %f", &num, name, &score[0], &score[1], &score[2]); if (num == 0) { break; }

// 分配内存空间p = (struct Student*)malloc(sizeof(struct Student));
if (p == NULL) {
  printf("内存分配失败\n");
  return 1;
}

// 填充学生信息
p->num = num;
for (int i = 0; i < 20; i++) {
  p->name[i] = name[i];
}
for (int i = 0; i < 3; i++) {
  p->score[i] = score[i];
}
p->next = NULL;  // 新节点的 next 指针指向 NULL

// 将新节点插入链表
if (head == NULL) {  // 如果是第一个节点,则设为头指针
  head = p;
} else {  // 如果不是第一个节点,则将新节点插入到尾节点的后面
  q->next = p;
}
q = p;  // 更新尾节点

}

// 遍历链表并输出学生信息 printf("学生信

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值