/定义学员结构体包括:学号(int),姓名,成绩(float),编写代码 (要求分函数实现) a) 创建多个学员数组并初始化 b) 添加一个学员,学号需要去重 c) 打印所有学员信息/采用单链表方式解决问题
时间: 2024-05-01 22:17:30 浏览: 85
这是一个关于编程的问题,可以回答。以下是参考代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义学员结构体
typedef struct Student {
int id;
char name[20];
float score;
struct Student *next;
} Student;
// 创建学员
Student *createStudent(int id, char *name, float score) {
Student *s = (Student *)malloc(sizeof(Student));
s->id = id;
strcpy(s->name, name);
s->score = score;
s->next = NULL;
return s;
}
// 添加学员
void addStudent(Student **pHead, Student *s) {
if (*pHead == NULL) {
*pHead = s;
return;
}
Student *p = *pHead;
while (p->next != NULL) {
if (p->id == s->id) {
printf("Duplicate id, please try again.\n");
return;
}
p = p->next;
}
if (p->id == s->id) {
printf("Duplicate id, please try again.\n");
return;
}
p->next = s;
}
// 打印学员
void printStudent(Student *pHead) {
Student *p = pHead;
while (p != NULL) {
printf("ID: %d, Name: %s, Score: %.1f\n", p->id, p->name, p->score);
p = p->next;
}
}
int main() {
// 创建学员数组并初始化
Student *s1 = createStudent(1, "Tom", 90.5);
Student *s2 = createStudent(2, "Jerry", 85.0);
Student *s3 = createStudent(3, "Bob", 92.5);
Student *s4 = createStudent(4, "Alice", 88.0);
Student *s5 = createStudent(5, "Lucy", 91.0);
// 添加学员
Student *pHead = NULL;
addStudent(&pHead, s1);
addStudent(&pHead, s2);
addStudent(&pHead, s3);
addStudent(&pHead, s4);
addStudent(&pHead, s5);
// 打印学员信息
printStudent(pHead);
return 0;
}
```
此代码实现了对学员信息的添加和打印,采用了单链表方式解决问题。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044947.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)