问题描述
我正在使用以下代码来实现一个包含字符串作为值的哈希表。 我知道我可以使用结构作为元素来对哈希表进行结构声明,如下所示:
struct Ht_item {
int key;
struct value *v; };
但是当我尝试修改这些函数时,它没有将结构体“值”正确复制到结构体项中。有人可以帮我解决这个问题吗?
Ht_item* create_item(int key,struct value *v) {
Ht_item* item = (Ht_item*) malloc (sizeof(Ht_item));
item->key = (int*) malloc (1*sizeof(key));
item->value = value *v = (value*)malloc(sizeof(value));
item->key= key;
item->value=value;
return item; }
void ht_insert(HashTable* table,int key,struct value *v) {
Ht_item* item = create_item(key,value);
unsigned long index = hash_function(key);
Ht_item* current_item = table->items[index];
if (current_item == NULL) {
if (table->count == table->size) {
printf("Insert Error: Hash Table is full\n");
free_item(item);
return;
}
table->items[index] = item;
table->count++;
}
else {
if (strcmp(current_item->key,key) == 0) {
table->items[index]->value= value;
return;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)