C++ 将插入的节点链接到struct c++;
该程序从csv创建哈希表。该函数仅适用于第一段数据,当我尝试链接新节点(链接链接)时,它无法附加新节点。我试图读取是否存在一个节点,以及是否需要遍历,直到找到结束点,然后将节点“事件”插入到链接列表中 感谢您的帮助C++ 将插入的节点链接到struct c++;,c++,pointers,linked-list,hashtable,singly-linked-list,C++,Pointers,Linked List,Hashtable,Singly Linked List,该程序从csv创建哈希表。该函数仅适用于第一段数据,当我尝试链接新节点(链接链接)时,它无法附加新节点。我试图读取是否存在一个节点,以及是否需要遍历,直到找到结束点,然后将节点“事件”插入到链接列表中 感谢您的帮助 struct hash_table_entry{ int event_id; int year; int event_index; struct hash_table_entry *next = nullptr; }; 这就是函数 bool inse
struct hash_table_entry{
int event_id;
int year;
int event_index;
struct hash_table_entry *next = nullptr;
};
这就是函数
bool insertHash(int index, hash_table_entry *event)
{
if(hashtable[index] == nullptr)
{
hashtable[index] = event;
return true;
}
hash_table_entry *pointingAt, *head;
// before we move
pointingAt = hashtable[index];
head = hashtable[index];
while(pointingAt->next)
{
pointingAt = pointingAt->next;
}
// insert
cout << pointingAt << "<- thing inserted" << endl;
pointingAt->next = event;
return true;
}
@user4581301通过添加新关键字以及更改附加节点的方式提供了帮助
hash_table_entry* c = new hash_table_entry
{
stoi(EVENT_ID),stoi(YEAR),
counter
};
1) 将链表与哈希表分开,以便可以单独实现和测试。2) 画画。您知道链表应该是什么样的,因此,如果您制作一系列图形,逐步显示插入过程,则可以将图形转换为代码,而无需太多麻烦。调试工作时,逐行通过代码并绘制所有指令。如果您绘制的内容与图片不匹配,您就知道哪里出了问题,并且可能对您应该做什么有了很好的想法。
while(pointingAt->next!=0)
和If(pointingAt->next==nullptr)
如果指向为空怎么办?@user4581301我假设它不会为空,因为上面它检查是否已经存在一个节点insertHash(hashCalc(事件ID,&c)
引发了一些我无法确认为真或假阳性的警报,但看起来您可能正在存储一个指向局部变量的指针。如果这是在循环中,您可能会反复使用同一个局部变量。@user4581301是的,它在循环中,循环csv文件中的每一行。如何使用新变量each运行
hash_table_entry* c = new hash_table_entry
{
stoi(EVENT_ID),stoi(YEAR),
counter
};