c++ - 如何在 C++ 中使用 for 循环声明 n 个链表?
问题描述
我试图从用户那里得到 n ,这将决定链表的数量。我将向这些链表添加元素,创建链表向量并将向量传递给 UDF。我无法使用 for 循环本身声明 n 个链表。
解决方案
根据我对问题的理解,也许这对你有用。
只需初始化一个向量并继续在其中推送新列表。
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
Node (int data) {
this->data = data;
this->next = nullptr;
}
};
vector<Node*> createLists(int n) {
vector<Node*> lists;
for (int i = 1; i <= n; i++) {
Node* node = new Node(100 + i);
lists.push_back(node);
}
return lists;
}
int main() {
int n;
cin >> n;
vector<Node*> lists = createLists(n);
return 0;
}
您可以创建一种insertElement
方法来在向量中的每个链表中添加项目
推荐阅读
- ios - 快速链接器命令失败,退出代码为 1 Google Maps iOS Util pod install
- postgresql - pg_restore:[存档]文件头中不支持的版本(1.13)
- android - 是否可以取消注册扩展类而不使其成为对象的广播接收器?
- html - 下载文件的链接不起作用,但会打开文件(html5)
- reactjs - react-bootstrap-table-next 没有类型脚本来支持导入
- java - 需要帮助设置我的 (java) Odata 服务以通过 SDK 将二进制文件发送到我的 SAP 系统(更新)
- ruby-on-rails - Rails - 转储请求标头给出无法转储匿名模块
- python - Python正则表达式从字符串中获取所有图像src'
- rest - 如何使用jmeter发送多个json正文?
- python - 如何每 10 秒将数据写入文件