我的哈希表使用此结构:
typedef struct bucket{
user *info;
bool available;
}bucket;
typedef struct user{
char nick[6];
int age;
}user;
typedef struct hashtable{
int size;
int elements;
bucket **buckets;
}hashtable;
我为我的哈希表创建了这个删除函数:
void delete_item(hashtable *HashTable, char *nick){
int position = contains(HashTable, nick);
if(position != -1){
HashTable->buckets[position]->available = true;
HashTable->buckets[position] = NULL;
HashTable->elements--;
}
}
该函数本身不会引发错误,但如果我删除一个项目,如果我尝试打印哈希表,则删除的位置会出现段错误。
删除的想法是将存储桶中的结构设置为 NULL 并将该存储桶设置为可再次插入,并且还可以用作 contains 函数的标记以继续查找下一个位置。
最佳答案
我怀疑这是你的问题:
HashTable->buckets[position] = NULL;
你是这个意思吗?
HashTable->buckets[position]->info = NULL
关于c - 哈希表删除方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50740113/