(8分,用链表实现对单生成睛进行样储,插人及显示功能. 学生包括工D和score属性。 结构体类型定义如下 struct grade int score cher 10L61 struct grade *next 红)写函教创建单向链表并存储学生成绩信息,有入TD和soore按退号隔开,先给入score后输人加,当score《0时链来创建宽成.并输出创建后的链表信息. <2)写函数实现在第r个结点后面插人一个新结点,并输出插入 新结点后的销表信息。 3)写函教释放链表所占内存空目 在上函教依次调用各函效即可

时间: 2024-03-03 12:47:09 浏览: 18
以下是实现要求的代码: ```c #include <stdio.h> #include <stdlib.h> struct grade { int ID; int score; struct grade *next; }; void create_list(struct grade **head) { int id, score; struct grade *new_node; *head = NULL; printf("Enter student ID and score (separated by space): "); scanf("%d %d", &id, &score); while (score >= 0) { new_node = (struct grade*)malloc(sizeof(struct grade)); new_node->ID = id; new_node->score = score; new_node->next = *head; *head = new_node; printf("Enter student ID and score (separated by space): "); scanf("%d %d", &id, &score); } } void insert_node(struct grade **head, int pos) { struct grade *new_node, *ptr; int i; ptr = *head; for (i = 1; i < pos && ptr != NULL; i++) { ptr = ptr->next; } if (ptr == NULL) { printf("Cannot insert node at position %d\n", pos); return; } printf("Enter student ID and score (separated by space): "); new_node = (struct grade*)malloc(sizeof(struct grade)); scanf("%d %d", &new_node->ID, &new_node->score); new_node->next = ptr->next; ptr->next = new_node; } void display_list(struct grade *head) { struct grade *ptr = head; while (ptr != NULL) { printf("ID: %d\tScore: %d\n", ptr->ID, ptr->score); ptr = ptr->next; } } void free_list(struct grade **head) { struct grade *ptr = *head; while (ptr != NULL) { *head = (*head)->next; free(ptr); ptr = *head; } } int main() { struct grade *head = NULL; int pos; create_list(&head); printf("Initial list:\n"); display_list(head); printf("Enter position to insert new node: "); scanf("%d", &pos); insert_node(&head, pos); printf("List after insertion:\n"); display_list(head); free_list(&head); return 0; } ``` 注:由于题目中没有规定 ID 的输入方式,所以这里采用了和 score 相同的方式,即在输入时用空格分隔。如果需要单独输入 ID,只需要稍作修改即可。

相关推荐

最新推荐

recommend-type

C语言实现带头结点的链表的创建、查找、插入、删除操作

主要介绍了C语言实现带头结点的链表的创建、查找、插入、删除操作方法,对于了解数据结构中链表的各项操作有很好的借鉴价值,需要的朋友可以参考下
recommend-type

Linux代码分析 本文详细分析了 2.6.x 内核中链表结构的实现,并通过实例对每个链表操作接口进行了详尽的讲解。

本文详细分析了 2.6.x 内核中链表结构的实现,并通过实例对每个链表操作接口进行了详尽的讲解
recommend-type

课设报告-“贪吃蛇_C语言_链表实现”-CSDN~.docx

课设报告-“贪吃蛇_C语言_链表实现~_课设”中 兑现将课设报告另发一篇博客~
recommend-type

学生成绩管理用C语言链表实现

学生成绩管理语言用C语言写的,用链表实现,包含查询模块,更新模块,输入输出模块,统计模块,文件读写模块,主程序。
recommend-type

对python实现合并两个排序链表的方法详解

今天小编就为大家分享一篇对python实现合并两个排序链表的方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【实战演练】MATLAB用遗传算法改进粒子群GA-PSO算法

![MATLAB智能算法合集](https://static.fuxi.netease.com/fuxi-official/web/20221101/83f465753fd49c41536a5640367d4340.jpg) # 2.1 遗传算法的原理和实现 遗传算法(GA)是一种受生物进化过程启发的优化算法。它通过模拟自然选择和遗传机制来搜索最优解。 **2.1.1 遗传算法的编码和解码** 编码是将问题空间中的解表示为二进制字符串或其他数据结构的过程。解码是将编码的解转换为问题空间中的实际解的过程。常见的编码方法包括二进制编码、实数编码和树形编码。 **2.1.2 遗传算法的交叉和
recommend-type

openstack的20种接口有哪些

以下是OpenStack的20种API接口: 1. Identity (Keystone) API 2. Compute (Nova) API 3. Networking (Neutron) API 4. Block Storage (Cinder) API 5. Object Storage (Swift) API 6. Image (Glance) API 7. Telemetry (Ceilometer) API 8. Orchestration (Heat) API 9. Database (Trove) API 10. Bare Metal (Ironic) API 11. DNS
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。