C 有两个哈希表,当删除其中一个哈希表中的项时,它会删除另一个哈希表中的项

C 有两个哈希表,当删除其中一个哈希表中的项时,它会删除另一个哈希表中的项,c,hashtable,C,Hashtable,我实现了一个哈希表,并初始化了两个不同的哈希表,如下所示: hashtable *active_users = create(); hashtable *inactive_users = create(); typedef struct user{ char nick[6]; char name[26]; int n_messages; bool occupied; }user; 上面哈希表的一个元素如下所示: hashtable *active_users

我实现了一个哈希表,并初始化了两个不同的哈希表,如下所示:

hashtable *active_users = create();
hashtable *inactive_users = create();
typedef struct user{
    char nick[6];
    char name[26];
    int n_messages;
    bool occupied;
}user;
上面哈希表的一个元素如下所示:

hashtable *active_users = create();
hashtable *inactive_users = create();
typedef struct user{
    char nick[6];
    char name[26];
    int n_messages;
    bool occupied;
}user;
在我的主程序中,我有一个功能,可以从
活动用户
中删除一个用户,并将其插入
非活动用户
,问题是当从
活动用户
中删除项目时,在将其插入
非活动用户
后,出于某种原因,它会同时将其删除

hashtable * delete_user(hashtable *active_users, hashtable *inactive_users, char *input_a){
    if(contains(active_users, input_a) != -1){
        user *tmp = get_item(active_users, input_a); //gets the user from the active_users hashtable
        if(load_factor(inactive_users)) //checks the hashtable size
            inactive_users = resize_HashTable(inactive_users); // if needed it resizes
        insert2(inactive_users, tmp); //insert in inactive_users
        print_table(inactive_users);
        printf("\n");
        delete_item(active_users, input_a); //deletes from active_users
        print_table(inactive_users);
        printf("+ user %s removed\n", input_a);
    }else{
        printf("+ user %s doesnt exist\n", input_a);
    }
    return inactive_users;
}
在上面的代码中,在插入新用户并从
活动用户中删除同一用户后,我打印了
非活动用户
哈希表,以便您可以看到问题

非活动用户插入后

i: 0, nick: me, name: mig
i: 1, nick: -, name: -
i: 2, nick: -, name: -
i: 3, nick: -, name: -
i: 4, nick: -, name: -
非活动用户
活动用户

i: 0, nick: -, name: -
i: 1, nick: -, name: -
i: 2, nick: -, name: -
i: 3, nick: -, name: -
i: 4, nick: -, name: -
要从哈希表中删除一个项目,我只需将其变量“accounted”标记为false,这意味着spot现在可以自由地用于插入。守则:

void delete_item(hashtable *HashTable, char *nick){
    int position = contains(HashTable, nick);
    if(position != -1){
        HashTable[position].buckets->occupied = false;
        HashTable->elements--;
    }
}

已占用
是结构
用户
的一个字段,您在
删除项目
时将其设置为false

相反,您应该将bucket设置为null

void delete_item(hashtable *HashTable, char *nick){
    int position = contains(HashTable, nick);
    if(position != -1){
        HashTable[position].buckets = null;
        HashTable->elements--;
    }
}

也许我应该在问题中发布它,但是bucket的定义是这样的:user*bucket,如果我将其设置为null,那么该位置将设置为null,我不能这样做,因为它会弄乱“contains”方法。我需要将一个位置标记为可自由使用,同时说那里有一个项目,因此在此之后继续搜索更多内容。在这种情况下,您应该创建一个用户副本,因为我假设将“占用”设置为false意味着清空它意味着该点现在可供使用,但是当我将它传输到非活动用户哈希表时,我没有将它设置为false,所以我不明白为什么要说它不是。所以我在两个不同的哈希表中使用指向结构的指针,当我在一个哈希表中更新它时,因为它是同一个指针,所以它会更新另一个哈希表中的值?