要求建立一个简单链表,它由3个学生数据的结点组成,每个结点包括3个域,分别为学号num、成绩score和next指针域,然后输出各结点中的数据。
时间: 2024-06-12 20:03:24 浏览: 10
以下是一个简单链表的代码示例:
```c++
#include <iostream>
using namespace std;
struct Student {
int num;
float score;
Student* next;
};
int main() {
// 创建3个学生结点
Student* s1 = new Student;
s1->num = 1;
s1->score = 90.5;
Student* s2 = new Student;
s2->num = 2;
s2->score = 85.5;
Student* s3 = new Student;
s3->num = 3;
s3->score = 92.0;
// 将结点串成链表
s1->next = s2;
s2->next = s3;
s3->next = NULL;
// 遍历链表并输出数据
Student* p = s1;
while (p != NULL) {
cout << "学号:" << p->num << ",成绩:" << p->score << endl;
p = p->next;
}
// 释放内存
delete s1;
delete s2;
delete s3;
return 0;
}
```
输出结果如下:
```
学号:1,成绩:90.5
学号:2,成绩:85.5
学号:3,成绩:92
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)