我不明白为什么我的hashTable无法正常工作C

时间:2018-06-10 19:29:21

标签: c hashmap hashtable linkedhashmap

我在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函数中遇到了问题。那或者我误解了正确使用双指针到节点。

1 个答案:

答案 0 :(得分:0)

我弄清楚数据没有链接的原因。如果密钥不匹配,它将转到else语句,并且该块中的第一个if语句询问hashTable-&gt; table [index] - &gt; next是否为NULL,当它应该询问hashTable-&gt; 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;

}
}