【发布时间】:2020-01-31 02:16:23
【问题描述】:
这是我在 c 中实现的 hashmap 及其初始化和插入代码。 在 hashmap_t 结构中,我使用指向包含键/值对的节点的指针数组(表)。在 hashmap_init 中,我分配所需数量的节点并循环遍历数组,将每个指针设置为 NULL。
我感到困惑的是 hashmap_put 函数。我找到了应该插入密钥的列表的索引,并且第一个指针由 hm->table[i] 引用。为了清楚起见,我想确保很明显 hm->table[i] 是列表的开头,因此我将其分配给 hashnode_t *head。
所以在插入第一个节点(head == NULL)时,我最初使用了 head = new_node,但我的插入都没有工作。它仅在我使用 hm->table[i] = new_node 时有效。
我不明白为什么会这样。 head 指向相同的东西,为什么设置 head 等于 new_node 不起作用?当 last->next = new_node 确实起作用时,我稍后在函数中也会感到困惑。 last 是一个指针,就像 head 一样,但它在那里工作。
感谢您的澄清。
typedef struct hashnode {
char key[128];
char val[128];
struct hashnode *next;
} hashnode_t;
typedef struct {
int item_count;
int table_size;
hashnode_t **table;
} hashmap_t;
void hashmap_init(hashmap_t *hm, int table_size) {
hm->table_size = table_size;
hm->item_count = 0;
hm->table = malloc(table_size * sizeof(hashnode_t));
for (int i = 0; i < table_size; i++) { // loop through array of pointers to nodes
hm->table[i] = NULL;
}
}
int hashmap_put(hashmap_t *hm, char key[], char val[]) {
hashnode_t *new_node = malloc(sizeof(hashnode_t)); // allocate new node
strcpy(new_node->key, key);
strcpy(new_node->val, val);
new_node->next = NULL;
int i = hashcode(key) % hm->table_size; // index of list hashed to
hashnode_t *head = hm->table[i];
hashnode_t *cur = head;
hashnode_t *last;
if (!head) { // list is empty
new_node->next = head;
hm->table[i] = new_node;
//why does head = new_node not work?
hm->item_count += 1;
return 1;
}
while (cur) { // loop through nodes
if (strcmp(cur->key, key) == 0) {
strcpy(cur->val, val);
free(new_node);
return 0;
}
last = cur; // save pointer to node that points to NULL
cur = cur->next;
}
last->next = new_node;
//why does it work here?
hm->item_count += 1;
return 1;
}
【问题讨论】:
标签: c linked-list hashmap