定义学生结构体struct Student类型如下: char id[10]; //学号 char Name[20];//姓名 double Score[3];//3门课程成绩 在主函数定义3个struct Student类型变量,并赋值,将3个变量插入到单链表的表头、表尾和指定位置用,C语言编写

时间: 2024-01-21 10:01:53 浏览: 30
好的,以下是C语言实现代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> struct Student { char id[10]; char Name[20]; double Score[3]; struct Student* next; }; // 在单链表表头插入节点 void InsertAtHead(struct Student** head, struct Student* newStudent) { newStudent->next = *head; *head = newStudent; } // 在单链表表尾插入节点 void InsertAtTail(struct Student** head, struct Student* newStudent) { if (*head == NULL) { *head = newStudent; } else { struct Student* temp = *head; while (temp->next != NULL) { temp = temp->next; } temp->next = newStudent; } } // 在单链表指定位置插入节点 void InsertAtPos(struct Student** head, struct Student* newStudent, int pos) { if (pos <= 1) { InsertAtHead(head, newStudent); } else { struct Student* temp = *head; int i = 1; while (temp != NULL && i < pos - 1) { temp = temp->next; i++; } if (temp == NULL) { printf("指定位置不存在!\n"); } else { newStudent->next = temp->next; temp->next = newStudent; } } } int main() { // 定义3个struct Student类型变量,并赋值 struct Student* student1 = (struct Student*)malloc(sizeof(struct Student)); strcpy(student1->id, "1001"); strcpy(student1->Name, "张三"); student1->Score[0] = 90; student1->Score[1] = 80; student1->Score[2] = 85; student1->next = NULL; struct Student* student2 = (struct Student*)malloc(sizeof(struct Student)); strcpy(student2->id, "1002"); strcpy(student2->Name, "李四"); student2->Score[0] = 95; student2->Score[1] = 85; student2->Score[2] = 90; student2->next = NULL; struct Student* student3 = (struct Student*)malloc(sizeof(struct Student)); strcpy(student3->id, "1003"); strcpy(student3->Name, "王五"); student3->Score[0] = 80; student3->Score[1] = 75; student3->Score[2] = 70; student3->next = NULL; // 将3个变量插入到单链表的表头、表尾和指定位置 struct Student* head = NULL; InsertAtHead(&head, student1); InsertAtTail(&head, student2); InsertAtPos(&head, student3, 2); // 遍历单链表并输出每个节点的信息 struct Student* temp = head; while (temp != NULL) { printf("学号:%s,姓名:%s,成绩:%.1f %.1f %.1f\n", temp->id, temp->Name, temp->Score[0], temp->Score[1], temp->Score[2]); temp = temp->next; } return 0; } ``` 输出结果与C++实现相同: ``` 学号:1001,姓名:张三,成绩:90.0 80.0 85.0 学号:1003,姓名:王五,成绩:80.0 75.0 70.0 学号:1002,姓名:李四,成绩:95.0 85.0 90.0 ```

相关推荐

最新推荐

recommend-type

2024嵌入式大厂面经C++首创

2024嵌入式大厂面经C++首创提取方式是百度网盘分享地址
recommend-type

C++ 高性能爬虫代码,带UI

C++ 高性能爬虫代码,带UI
recommend-type

2024嵌入式面试资料裕日软件C笔试题

2024嵌入式面试资料裕日软件C笔试题提取方式是百度网盘分享地址
recommend-type

黑色素瘤分类数据集10015张7类别.7z

数据集类型:图像分类用,不可用于目标检测无标注文件 数据集格式:仅仅包含jpg图片,每个类别文件夹下面存放着对应图片 图片数量(jpg文件个数):10015 分类类别数:7 类别名称:[“0”,“1”,“2”,“3”,“4”,“5”,“6”] 更多信息:blog.csdn.net/FL1623863129/article/details/139561265
recommend-type

2024年高尿酸及痛风疾病医药行业分析报告.pptx

行业报告
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正态分布协方差分析:揭示正态分布变量之间的协方差

![MATLAB正态分布协方差分析:揭示正态分布变量之间的协方差](https://site.cdn.mengte.online/official/2021/11/20211128213137293.png) # 1. 正态分布概述 正态分布,又称高斯分布,是统计学中最重要的连续概率分布之一。它广泛应用于自然科学、社会科学和工程领域。 正态分布的概率密度函数为: ``` f(x) = (1 / (σ√(2π))) * exp(-(x - μ)² / (2σ²)) ``` 其中: - μ:正态分布的均值 - σ:正态分布的标准差 - π:圆周率 正态分布具有以下特性: - 对称性:
recommend-type

我正在开发一款个人碳足迹计算app,如何撰写其需求分析文档,请给我一个范例

为了更全面、清晰地定义个人碳足迹计算app的需求,需求分析文档应该包含以下内容: 1.项目简介:对该app项目的概述及目标进行说明。 2.用户分析:包括目标用户群、用户需求、行为等。 3.功能需求:对app的基本功能进行定义,如用户登录、数据录入、数据统计等。 4.非功能需求:对使用app的性能和质量等进行定义,如界面设计、数据安全、可扩展性等。 5.运行环境:包括app的开发环境和使用环境。 下面是一个范例: 需求分析文档 1. 项目简介 该app项目旨在为用户提供一款方便、易用、可定制的个人碳足迹计算平台,以促进环保和可持续性发展。 2. 用户分析 目标用户群:全球关
recommend-type

JSBSim Reference Manual

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