哈希表删除方法
Hashtable delete method
我的哈希表使用这个结构:
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
我的哈希表使用这个结构:
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