我目前有一个已经创建的链表,需要一个指向列表中每个项目的指针数组,以便对其进行排序和操作。
using namespace std;
struct Node {
float tries;
float vals;
Node* next;
Node* prev;
};
class doList {
public:
doList(float attempts[], float values[], int numElements) {
Node* current;
current = new Node;
current->tries = attempts[0];
current->vals = values[0];
current->prev = NULL;
head = current;
tail = current;
for (int d = 1; d < numElements; d++) {
current = new Node;
current->tries = attempts[d];
current->vals = values[d];
current->prev = tail;
tail->next = current;
tail = current;
if (d == (numElements - 1)) {
tail->next = NULL;
}
}
arrayPointers = new Node*[numElements];
}
private:
Node* head;
Node* tail;
Node** arrayPointers;
};
我试图使用一个私有元素来创建指针数组并为其分配空间,但除此之外,我不确定如何使用它。必须在不包括任何其他选项的情况下进行。
编辑很抱歉我不够清楚。我对如何使用指向链表的指针数组感到困惑。我需要帮助弄清楚如何将节点存储在指针数组中。本质上,我需要一个数组,其中数组中的元素指向列表中的条目。
您需要预先创建数组,并在创建时存储指向每个元素的指针。
doList(float attempts[], float values[], int numElements) {
arrayPointers = new Node*[numElements];
Node* current;
current = new Node;
current->tries = attempts[0];
current->vals = values[0];
current->prev = NULL;
head = current;
tail = current;
arrayPointers[0] = current;
for (int d = 1; d < numElements; d++)
{
current = new Node;
current->tries = attempts[d];
current->vals = values[d];
current->prev = tail;
tail->next = current;
tail = current;
arraypointers[d] = current;
if (d == (numElements - 1))
{
tail->next = NULL;
}
}
}