gpt4 book ai didi

c - 我不明白为什么我的 hashTable 不能正常工作 C

转载 作者:行者123 更新时间:2023-11-30 20:31:28 24 4
gpt4 key购买 nike

我正在用 C 语言创建一个哈希表,它使用 Node 双指针并使用单独的链接来解决冲突,但是当我运行代码时,它不会将冲突放入链接列表中。

HTable *createTable(size_t size, int (*hashFunction)(size_t tableSize, 
int key),void (*destroyData)(void *data),void (*printData)(void
*toBePrinted)){
HTable * h = malloc(sizeof(HTable));

h->size = size;
h->destroyData = destroyData;
h->hashFunction = hashFunction;
h->printData = printData;

h->table = malloc(h->size * sizeof(Node*));
for(int i = 0; i < h->size; i++){

h->table[i] = malloc(sizeof(Node));
h->table[i]->key = 0;
h->table[i]->data = NULL;
h->table[i]->next = NULL;
}

return h;
}

Node *createNode(int key, void *data){
Node * n = malloc(sizeof(Node));

n->key = key;
n->data = data;
n->next = NULL;

return n;
}

void insertData(HTable *hashTable, int key, void *data){
if(hashTable != NULL){
Node * n = createNode(key, data);
int index = hashTable->hashFunction(hashTable->size, key);

if(hashTable->table[index] != NULL)
if(hashTable->table[index]->key == key){
if(hashTable->table[index]->next != NULL){
n->next = hashTable->table[index]->next;
hashTable->table[index] = n;
}
else
hashTable->table[index] = n;
}
else{
if(hashTable->table[index]->next != NULL){
Node * itr = hashTable->table[index];
while(itr->next != NULL){
itr = itr->next;
}
itr->next = n;
}
else
hashTable->table[index] = n;
}
else{
hashTable->table[index] = n;
}
}
}

HTable 和 Node 看起来像这样:

typedef struct Node
{
int key;
void *data;
struct Node *next;
} Node;

typedef struct HTable
{
size_t size;
Node **table;
void (*destroyData)(void *data);
int (*hashFunction)(size_t tableSize, int key);
void (*printData)(void *toBePrinted);
}HTable;

我认为当我使用迭代器查找链接列表中的最后一项时,我的 insertData 函数遇到了问题。或者我误解了指向节点的双指针的正确使用。

最佳答案

我弄清楚了为什么数据没有链接。如果键不匹配,它将转到 else 语句,并且该 block 中的第一个 if 语句询问 hashTable->table[index]->next 是否不为 NULL,而它应该询问 hashTable->table[index ] 为 NULL。这是因为可能存在一个节点,而其下一个节点将指向 NULL,然后数据将被覆盖。感谢您的回复,我添加了评论,这是很好的建议,因为它帮助我找到了错误。所以谢谢你@wildplasser

如果有人想知道,这里是代码:

void insertData(HTable *hashTable, int key, void *data){
if(hashTable != NULL){
Node * n = createNode(key, data);
int index = hashTable->hashFunction(hashTable->size, key);

//Check if hashTable table node contains data
if(hashTable->table[index]->data != NULL)
//Check if the key at that index matches the incoming key
if(hashTable->table[index]->key == key){
//checks if there is more than one node in the chain and replaces the
//first piece of data in the chain
if(hashTable->table[index] != NULL){
n->next = hashTable->table[index]->next;
hashTable->table[index] = n;
}
}
//if the keys do not match
else{
//check if their is one node in the chain
if(hashTable->table[index] != NULL){
Node * itr = hashTable->table[index];
//iterate through the chain to last node
while(itr->next != NULL){
itr = itr->next;
}
//insert node into end of chain
itr->next = n;
}
}
//if there is no data in the node insert the data
else
hashTable->table[index] = n;

}
}

关于c - 我不明白为什么我的 hashTable 不能正常工作 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787309/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com