用C语言构建HashMap的问题
当我尝试使用 C 构建 hashMap 时,我遇到了问题。我的目标是构建一个附加了链表数组的 hashMap。
我现在正在研究插入元素功能,这是奇怪的部分。我希望在开头插入一个newNode。当我使用临时节点头来存储此链表的“头”并尝试插入时,它失败并且没有值插入。但是如果我直接将头更改为“_hashmap->arrayOfLists[bucket]”,那么它就可以工作..我不知道为什么,我认为有一个临时节点应该可以工作..任何人都可以帮忙解决这个问题吗?谢谢!
typedef 结构对{ 字符*键; 字符*值; }pair_t;
typedef 结构节点{ 结构节点*下一个; pair_t* kv;}node_t;
[原始有问题的代码]
void hashmap_insert(hashmap_t* _hashmap,char* 键,char* 值){
pair_t* newPair = (pair_t*)malloc(sizeof(pair_t)); newPair->key = (char*)malloc(strlen(key) * sizeof(char)+1); newPair->value = (char*)malloc(strlen(value) * sizeof(char)+1); strcpy(newPair->key, key); //将输入键复制到malloc空间中。 strcpy(newPair->value, value); //将输入值复制到malloc空间中。 //创建一个要插入的newNode。 node_t* newNode = (node_t*)malloc(sizeof(node_t)); newNode->kv = newPair; newNode->next = NULL; unsigned int Bucket = _hashmap->hashFunction(key, _hashmap->buckets); node_t* head = _hashmap->arrayOfLists[bucket]; // 如果桶是空的 如果(头== NULL){ 头=新节点; } else {//在前面插入newNode。 newNode->next = head; 头=新节点; }
}
【有效的代码】
if(_hashmap->arrayOfLists[bucket] == NULL) { _hashmap->arrayOfLists[bucket] = newNode; } 别的 { newNode->next = _hashmap->arrayOfLists[bucket]; _hashmap->arrayOfLists[bucket] = newNode; }
I've encountered problems when I'm trying to build a hashMap using C. My goal is to build a hashMap with an array of linkedlist appended.
I'm now working on the insert element function, and here is the strange part. I hope to insert a newNode at the beginning. When I use a temp node head to store the "head" of this linkedlist and try to insert, it fails with no value insert. But if I change the head directly to "_hashmap->arrayOfLists[bucket]", then it works.. I do not know why and I think having a temp node should work.. Anyone can help with this? Thanks!
typedef struct pair{
char* key;
char* value; }pair_t;typedef struct node{
struct node* next;
pair_t* kv;}node_t;
[Original problematic code]
void hashmap_insert (hashmap_t* _hashmap, char* key, char* value){
pair_t* newPair = (pair_t*)malloc(sizeof(pair_t)); newPair->key = (char*)malloc(strlen(key) * sizeof(char)+1); newPair->value = (char*)malloc(strlen(value) * sizeof(char)+1); strcpy(newPair->key, key); //copy the input key into the malloc space. strcpy(newPair->value, value); //copy the input value into the malloc space. //create a newNode that is to be inserted. node_t* newNode = (node_t*)malloc(sizeof(node_t)); newNode->kv = newPair; newNode->next = NULL; unsigned int bucket = _hashmap->hashFunction(key, _hashmap->buckets); node_t* head = _hashmap->arrayOfLists[bucket]; // if the bucket is empty if(head == NULL) { head = newNode; } else {//insert the newNode at the front. newNode->next = head; head = newNode; }
}
【The code that works]
if(_hashmap->arrayOfLists[bucket] == NULL) { _hashmap->arrayOfLists[bucket] = newNode; } else { newNode->next = _hashmap->arrayOfLists[bucket]; _hashmap->arrayOfLists[bucket] = newNode; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论