所以我现在有一个简单的结构(链表),我将使用在一个HashMap:创建指针动态分配的数组到结构C++
struct Node {
std::string key, value;
Node* head;
}
我目前试图动态地分配与指针的数组到每个结构。这就是我现在所拥有的......
Node* nodes = new Node[100]
我明白这个分配到内存中100个节点(我将不得不删除以后)的阵列;然而,在反复尝试横向这些节点(这是我的实现作为一个链表)...
for (int x = 0; x < 100; x++) {
Node current = nodes[x]; // Problem is I wanted an array to node pointers. This is not a pointer.
while (current != nullptr) { // this isn't even legal since current is not a pointer.
// DO STUFF HERE
current = current.next; // This is not a pointer access to a method. I'm looking to access next with current->next;
}
}
希望我是很清晰。有人可以如何分配一个指向结构的动态数组指针?到目前为止,我可以动态地分配一个结构数组,而不是指向结构体的指针数组。
基本上,通过为您的节点提供连续的内存,您炸毁了整个链接列表的想法。由于您拥有空间信息,因此不需要下一个节点(即,index) – 101010
你似乎不确定你是想要一个链表还是一个动态数组。 – PaulMcKenzie
这个想法是我正在实现一个hashmap,因此需要一个动态分配的LinkedLists数组。 –