建立一个有3个学生数据结点构成的静态链表,然后输出各结点中的数据 用c++
时间: 2024-04-05 07:31:54 浏览: 23
好的,以下是代码实现:
```c++
#include <iostream>
using namespace std;
struct Student {
int id;
string name;
int age;
};
struct Node {
Student data;
int next; // 指向下一个结点的位置
};
int main() {
Node students[3]; // 定义3个结点的静态链表
students[0].data = {1, "张三", 18};
students[1].data = {2, "李四", 19};
students[2].data = {3, "王五", 20};
students[0].next = 1; // 第一个结点指向第二个结点
students[1].next = 2; // 第二个结点指向第三个结点
students[2].next = -1; // 最后一个结点指向空
int p = 0; // 从第一个结点开始遍历链表
while (p != -1) {
cout << "学号:" << students[p].data.id << endl;
cout << "姓名:" << students[p].data.name << endl;
cout << "年龄:" << students[p].data.age << endl << endl;
p = students[p].next; // 指向下一个结点
}
return 0;
}
```
输出结果如下:
```
学号:1
姓名:张三
年龄:18
学号:2
姓名:李四
年龄:19
学号:3
姓名:王五
年龄:20
```
希望能帮到你!如果有任何问题可以继续问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)