c - 哈希表删除方法

标签 c hashtable

我的哈希表使用此结构:

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/

相关文章:

c - 在 C 中使用指针循环遍历结构元素

c# - 将 hashtable.Keys 转换为 List<int> 或其他 IEnumerable<int>

C语言无法让strcmp工作

c - 消息队列多接收者实现

C - 分配内存并将字符串复制到哈希表的数组中

.net - 为什么 Dictionary.First() 这么慢?

algorithm - Lisp gethash 复杂性

c++ - c++-使用unordered_map解决2和的方法

c - 最长的字符串

c - 链接到 openCL 内核程序中的外部库