在两个哈希表之间移动项目
Moving items between two hashtables
我正在尝试制作一个程序,用于从我制作的哈希表中插入和删除项目(哈希表工作正常)。该程序有一个主菜单,如果输入正确,它将调用 insert
和 delete
函数。
该程序包含 2 个哈希表:
active_users
哈希表
inactive_users
哈希表
当用户 "deleted" 时,它将用户从活跃用户哈希表移动到非活跃用户哈希表。
问题:
要将用户从活动哈希表移动到非活动哈希表,我首先从活动哈希表中获取用户信息,并将其插入非活动哈希表中。然后我将其从活动哈希表中删除。但是,出于某种原因,它也会从非活动哈希表中删除,从而导致错误。
注:函数insert2
、delete_item
和contains
都是哈希表函数,调用它们来执行哈希表内的操作.这些函数都运行良好,所以我不认为问题出在哈希表实现上。
typedef struct user{
char nick[6];
char name[26];
bool occupied;
}user;
void insert_user(hashtable *active_users, hashtable *inactive_users, char *input_a, char *input_b){
user *new_user = malloc(sizeof(user)); //initializes a new user
strcpy(new_user->name, input_b);
strcpy(new_user->nick, input_a);
new_user->occupied = true;
if(contains(active_users, input_a) == -1 && contains(inactive_users, input_a) == -1){
if(load_factor(active_users)){ //checks the size of the hashtable
resize_HashTable(active_users); // if true, resizes
}
insert2(active_users, new_user); //insert user in actives
printf("+ user %s created\n", input_a);
}else{
printf("+ nick %s already used\n", input_a);
}
}
void 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); //get the user from active
insert2(inactive_users, tmp); //insert in inactives
delete_item(active_users, input_a); //delete from active
printf("+ user %s removed\n", input_a);
}else{
printf("+ user %s doesnt exist\n", input_a);
}
}
int main(){
hashtable *active_users = create();
hashtable *inactive_users = create();
char buffer[37];
char tipo;
char input_a[6];
char input_b[26];
while(fgets(buffer, 37, stdin)){
sscanf(buffer, "%c %s %[^\n]s", &tipo, input_a, input_b);
switch(tipo) {
case 'U' :
insert_user(active_users, inactive_users, input_a, input_b);
break;
case 'R' :
delete_user(active_users, inactive_users, input_a);
break;
default :
printf("Invalid Operation\n");
}
}
return 0;
}
根据要求哈希表函数:
void insert2(hashtable *HashTable, user *a){
int hash_value = hash(a->nick);
int new_position = hash_value % HashTable->size;
if (new_position < 0) new_position += HashTable->size;
int position = new_position;
while (HashTable->buckets[position]->occupied && position != new_position - 1) {
position++;
position %= HashTable->size;
}
a->occupied = true;
HashTable->buckets[position] = a;
HashTable->elements++;
}
void delete_item(hashtable *HashTable, char *nick){
int position = contains(HashTable, nick);
if(position != -1){
HashTable->buckets[position]->occupied = false;
}
HashTable->elements--;
}
如有任何帮助,我们将不胜感激。
这些是一些观察结果。我认为你应该认真地重新设计你的代码。在我看来,你应该只有一个函数 move_user
将用户从一个 table 移动到另一个并将它占用的桶设置为空(因此从 occupied
中删除标志 user
结构,不属于那里):
在 insert2
中你添加一个元素,即使它已经存在(你不检查它)。
在 delete_item
中,即使找不到元素,您也会减少元素的数量。
在 insert2
中,您覆盖了现有项目,因此您泄漏了内存。
在 insert2
中,您不检查桶中是否有元素。如果 bucket 为空,check
while (HashTable->buckets[position]->occupied
可能会导致段错误。
至于您报告的问题:当您
user *tmp = get_item(active_users, input_a);
你得到一个指向用户的指针。然后,您将指向的记录插入到非活动散列 table 中,并从活动散列 table 中插入 "remove"。但是,您的 "removing" 只是将用户记录 中的占用标志 设置为零,因此现在不会在其他哈希 table 中找到它。如上所述,此标志不属于用户数据结构。
我正在尝试制作一个程序,用于从我制作的哈希表中插入和删除项目(哈希表工作正常)。该程序有一个主菜单,如果输入正确,它将调用 insert
和 delete
函数。
该程序包含 2 个哈希表:
active_users
哈希表inactive_users
哈希表
当用户 "deleted" 时,它将用户从活跃用户哈希表移动到非活跃用户哈希表。
问题: 要将用户从活动哈希表移动到非活动哈希表,我首先从活动哈希表中获取用户信息,并将其插入非活动哈希表中。然后我将其从活动哈希表中删除。但是,出于某种原因,它也会从非活动哈希表中删除,从而导致错误。
注:函数insert2
、delete_item
和contains
都是哈希表函数,调用它们来执行哈希表内的操作.这些函数都运行良好,所以我不认为问题出在哈希表实现上。
typedef struct user{
char nick[6];
char name[26];
bool occupied;
}user;
void insert_user(hashtable *active_users, hashtable *inactive_users, char *input_a, char *input_b){
user *new_user = malloc(sizeof(user)); //initializes a new user
strcpy(new_user->name, input_b);
strcpy(new_user->nick, input_a);
new_user->occupied = true;
if(contains(active_users, input_a) == -1 && contains(inactive_users, input_a) == -1){
if(load_factor(active_users)){ //checks the size of the hashtable
resize_HashTable(active_users); // if true, resizes
}
insert2(active_users, new_user); //insert user in actives
printf("+ user %s created\n", input_a);
}else{
printf("+ nick %s already used\n", input_a);
}
}
void 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); //get the user from active
insert2(inactive_users, tmp); //insert in inactives
delete_item(active_users, input_a); //delete from active
printf("+ user %s removed\n", input_a);
}else{
printf("+ user %s doesnt exist\n", input_a);
}
}
int main(){
hashtable *active_users = create();
hashtable *inactive_users = create();
char buffer[37];
char tipo;
char input_a[6];
char input_b[26];
while(fgets(buffer, 37, stdin)){
sscanf(buffer, "%c %s %[^\n]s", &tipo, input_a, input_b);
switch(tipo) {
case 'U' :
insert_user(active_users, inactive_users, input_a, input_b);
break;
case 'R' :
delete_user(active_users, inactive_users, input_a);
break;
default :
printf("Invalid Operation\n");
}
}
return 0;
}
根据要求哈希表函数:
void insert2(hashtable *HashTable, user *a){
int hash_value = hash(a->nick);
int new_position = hash_value % HashTable->size;
if (new_position < 0) new_position += HashTable->size;
int position = new_position;
while (HashTable->buckets[position]->occupied && position != new_position - 1) {
position++;
position %= HashTable->size;
}
a->occupied = true;
HashTable->buckets[position] = a;
HashTable->elements++;
}
void delete_item(hashtable *HashTable, char *nick){
int position = contains(HashTable, nick);
if(position != -1){
HashTable->buckets[position]->occupied = false;
}
HashTable->elements--;
}
如有任何帮助,我们将不胜感激。
这些是一些观察结果。我认为你应该认真地重新设计你的代码。在我看来,你应该只有一个函数 move_user
将用户从一个 table 移动到另一个并将它占用的桶设置为空(因此从 occupied
中删除标志 user
结构,不属于那里):
在 insert2
中你添加一个元素,即使它已经存在(你不检查它)。
在 delete_item
中,即使找不到元素,您也会减少元素的数量。
在 insert2
中,您覆盖了现有项目,因此您泄漏了内存。
在 insert2
中,您不检查桶中是否有元素。如果 bucket 为空,check
while (HashTable->buckets[position]->occupied
可能会导致段错误。
至于您报告的问题:当您
user *tmp = get_item(active_users, input_a);
你得到一个指向用户的指针。然后,您将指向的记录插入到非活动散列 table 中,并从活动散列 table 中插入 "remove"。但是,您的 "removing" 只是将用户记录 中的占用标志 设置为零,因此现在不会在其他哈希 table 中找到它。如上所述,此标志不属于用户数据结构。