使用一个结构重新哈希链表并调整其大小



我使用rehash / resize函数创建了一个简单的链式哈希表,但是它似乎不起作用。我不确定问题出在哪里。

我想在一个键上存储5个以上元素时重新哈希。

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

感谢您的帮助

#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");
}
lx53166753 回答:使用一个结构重新哈希链表并调整其大小

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

当前,当您重新哈希时,您将在本地new_hash_table数组中构建新表。您将其填充,然后在函数末尾将其丢弃(释放分配给节点的所有内存)。

您的哈希函数采用固定大小的原始数组,并且当您在重新哈希过程中做很多额外的工作时,这是不必要的,只会降低性能。

本文链接:https://www.f2er.com/3131065.html

大家都在问