c++要求建立一个有3个学生数据结点构成的静态链表,然后输出各结点中的数据
时间: 2024-04-02 10:33:13 浏览: 83
c++ 语言 实现静态链表
5星 · 资源好评率100%
好的,下面是实现代码:
```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, "Tom", 18 };
students[0].next = 1;
students[1].data = { 2, "Jerry", 20 };
students[1].next = 2;
students[2].data = { 3, "Alice", 19 };
students[2].next = -1;
for (int i = 0; i < 3; i++) {
cout << "Student " << i + 1 << ":\n";
cout << "ID: " << students[i].data.id << endl;
cout << "Name: " << students[i].data.name << endl;
cout << "Age: " << students[i].data.age << endl;
cout << endl;
}
return 0;
}
```
运行结果:
```
Student 1:
ID: 1
Name: Tom
Age: 18
Student 2:
ID: 2
Name: Jerry
Age: 20
Student 3:
ID: 3
Name: Alice
Age: 19
```
注:静态链表是指使用数组实现链表结构,相比于动态链表(使用指针实现),静态链表的空间利用率更高,但是插入和删除操作比较麻烦。
阅读全文