gpt4 book ai didi

c - 使用一个结构重新散列链式哈希表并调整其大小

转载 作者:行者123 更新时间:2023-11-30 16:10:51 25 4
gpt4 key购买 nike



我创建了一个带有重新哈希/调整大小函数的简单链接哈希表,但它似乎不起作用。我不确定问题出在哪里。

当一个键存储了超过 5 个元素时,我想重新哈希。

我知道调试器是我的 friend ,我尝试使用调试器找到问题,但没有成功。我知道这可能不是有史以来最好的哈希表,但这是我第一次这样做。

感谢您的帮助

#include <stdio.h>
#include <stdlib.h>

#define MAX 10
#define CAPACITY 5
#define FACTOR 0.7 // not using currently

typedef struct hast_table_item
{
int data;
int key;
struct hast_table_item *next_data;
}HASH_TABLE_ITEM;

int hash_function(int value)
{
return value % MAX;
}

void display_hash_table(HASH_TABLE_ITEM **hash_table);

void rehash(HASH_TABLE_ITEM **old_hash_table) {
int current_capacity = MAX;
int old_capacity = current_capacity;
int new_capacity = old_capacity*2;
HASH_TABLE_ITEM *new_hash_table[new_capacity];
int i_my_hash;

/* Clear the table */
for (i_my_hash=0; i_my_hash<new_capacity; i_my_hash++) {
new_hash_table[i_my_hash] = NULL;
}

/* Do Rehash */
for (i_my_hash=0; i_my_hash<old_capacity; i_my_hash++) {
if (old_hash_table[i_my_hash]->key != NULL) { // Guess there is problem in this if statement
insert_hti(new_hash_table, old_hash_table[i_my_hash]->data);
}
}
display_hash_table(new_hash_table);
}

void check_capacity(HASH_TABLE_ITEM **hash_table, HASH_TABLE_ITEM *new_item)
{
int count = 0;
HASH_TABLE_ITEM *current = new_item;
int i;

for (i=0; i<MAX; i++) {
if (hash_table[i] != NULL) {
while (current != NULL) {
count++;
current = current->next_data;
if (count == 5) {
printf("--- REHASHING ---\n");
rehash(hash_table);
// count = 0;
break;
}
}
}
}
printf("Key %d has %d elements.\n",new_item->key, count);
}

void insert_hti(HASH_TABLE_ITEM **hash_table, int value)
{
HASH_TABLE_ITEM *new_item = NULL;
new_item = (HASH_TABLE_ITEM*)malloc(sizeof(HASH_TABLE_ITEM));

new_item->data = value;
new_item->key = hash_function(value);
new_item->next_data = NULL;

if (hash_table[new_item->key] == NULL) {
hash_table[new_item->key] = new_item;
check_capacity(hash_table,new_item);
}
else {
new_item->next_data = hash_table[new_item->key];
hash_table[new_item->key] = new_item;
check_capacity(hash_table,new_item);
}

}

void display_hash_table(HASH_TABLE_ITEM **hash_table)
{
HASH_TABLE_ITEM *current = NULL;
int i;

for (i=0; i<MAX; i++) {
if (hash_table[i] != NULL) {
current = hash_table[i];
printf("Table Key (%d) --> ", i);

while (current != NULL) {
printf(" | Data: %d |", current->data);
current = current->next_data;
}
printf("\n");
}
}
}

void main() {
/* Declare My Hash */
int insert_value_my_hash = 0;
int i_my_hash;
HASH_TABLE_ITEM* hash_table[MAX];

/* Clear the table */
for (i_my_hash=0; i_my_hash<MAX; i_my_hash++) {
hash_table[i_my_hash] = NULL;
}

/* Insert into My Hash */
while (scanf("%d", &insert_value_my_hash) == 1) {
insert_hti(hash_table, insert_value_my_hash);
printf("Vložila som element %d do mojej Hash tabuľky.\n", insert_value_my_hash);
}
display_hash_table(hash_table);
printf("\n\n");
}

最佳答案

您正在为哈希表(原始的和重新哈希的)使用局部变量数组,但需要分配这些数组并传递指针。

目前,当您重新哈希时,您会在本地 new_hash_table 数组中构建新表。您填充它,然后在函数结束时将其丢弃(泄漏为节点分配的所有内存)。

您的哈希函数假定原始数组大小固定,当您在重新哈希期间执行大量额外工作时,这是不必要的,只会降低性能。

关于c - 使用一个结构重新散列链式哈希表并调整其大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58784065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com