1.建立一个链表将实训12第2题定义的 学生数据存入链表,并读出显示出来。(10分) 数据为: { {"2020B200301","刘滨","男",18,{2020,9,20},70,85}, {"2020B200302","阚美娟","女",17,{2020,9,20},87,95}, {"2020B200303","胡大勇","男",17,{2020,9,20},69,80}, {"2020B200305","黎丽","女",18,{2020,9,20},68,88}, {"2020B200309","金鑫","男",19,{2019,9,1},90,85} }.{“2020B200303”,”胡大勇”,”男”,17,{2020,9,20},69,80}
时间: 2023-07-19 14:08:20 浏览: 74
学生信息建立采用链表结构
好的,以下是代码实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
struct Student {
char id[20]; // 学号
char name[20]; // 姓名
char gender[5]; // 性别
int age; // 年龄
int birthday[3]; // 生日,0-年,1-月,2-日
int score1; // 第一科成绩
int score2; // 第二科成绩
Student *next; // 指向下一个节点的指针
};
void printStudent(Student *s) {
cout << "学号:" << s->id << endl;
cout << "姓名:" << s->name << endl;
cout << "性别:" << s->gender << endl;
cout << "年龄:" << s->age << endl;
cout << "生日:" << s->birthday[0] << "-" << s->birthday[1] << "-" << s->birthday[2] << endl;
cout << "第一科成绩:" << s->score1 << endl;
cout << "第二科成绩:" << s->score2 << endl;
}
int main() {
// 初始化数据
Student s1 = {"2020B200301", "刘滨", "男", 18, {2020, 9, 20}, 70, 85, NULL};
Student s2 = {"2020B200302", "阚美娟", "女", 17, {2020, 9, 20}, 87, 95, NULL};
Student s3 = {"2020B200303", "胡大勇", "男", 17, {2020, 9, 20}, 69, 80, NULL};
Student s4 = {"2020B200305", "黎丽", "女", 18, {2020, 9, 20}, 68, 88, NULL};
Student s5 = {"2020B200309", "金鑫", "男", 19, {2019, 9, 1}, 90, 85, NULL};
// 将节点串起来
s1.next = &s2;
s2.next = &s3;
s3.next = &s4;
s4.next = &s5;
// 遍历链表并输出数据
Student *p = &s1;
while (p != NULL) {
if (strcmp(p->id, "2020B200303") == 0) {
// 找到了目标节点,输出数据并退出循环
printStudent(p);
break;
}
p = p->next;
}
return 0;
}
```
输出结果如下:
```
学号:2020B200303
姓名:胡大勇
性别:男
年龄:17
生日:2020-9-20
第一科成绩:69
第二科成绩:80
```
阅读全文