【问题标题】:I'm not understanding why my hashTable is not working properly C我不明白为什么我的 hashTable 不能正常工作 C
【发布时间】:2018-11-20 02:08:04
【问题描述】:

我在 C 中创建一个 HashTable,它使用 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 函数遇到了问题。那或者我误解了正确使用指向节点的双指针。

【问题讨论】:

  • 对不起,这是在 C 中
  • if(hashTable-&gt;table[index]-&gt;key == key) 为真时,你扔掉旧链,只把新节点放在那里,在我看来。
  • 对于散列值使用无符号类型也是一个强烈的建议。您不希望在 hashTable-&gt;table[index] 中出现负索引。
  • @ArndtJonasson 的想法是每个键只有一组数据,因此如果有 2 个匹配的键我替换数据,但如果 2 个不同的键导致同一个 bin,我将其添加到链条
  • 是的,你替换那个节点,然后扔掉它的next指向的东西。

标签: c hashmap hashtable linkedhashmap


【解决方案1】:

我知道为什么数据没有链接。如果键不匹配,它将转到 else 语句,并且该块中的第一个 if 语句询问 hashTable->table[index]->next 是否不是 NULL,而它应该询问是否 hashTable->table[index ] 为 NULL。这是因为可能有一个节点,而它的下一个节点将指向 NULL,然后数据将被覆盖。感谢您的回复,我添加了 cmets,这是很好的建议,因为它帮助我找到了错误。所以谢谢@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;

}
}

【讨论】:

    猜你喜欢
    • 2019-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 1970-01-01
    • 2021-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多