建立指针数组并使用循环语句完成建立存储1-10的链表
时间: 2023-05-30 19:02:51 浏览: 126
循环链表的实现
下面是C++代码示例:
```
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
int main() {
// 建立指针数组
const int SIZE = 10;
Node* arr[SIZE];
// 建立链表
for (int i = 0; i < SIZE; i++) {
arr[i] = new Node;
arr[i]->data = i + 1;
if (i == SIZE - 1) {
arr[i]->next = NULL;
} else {
arr[i]->next = arr[i + 1];
}
}
// 输出链表
Node* current = arr[0];
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
cout << endl;
// 释放内存
for (int i = 0; i < SIZE; i++) {
delete arr[i];
}
return 0;
}
```
输出结果为:
```
1 2 3 4 5 6 7 8 9 10
```
阅读全文