c - 链式哈希表的问题

标签 c hash struct chained

我在弄清楚如何正确执行此操作时遇到了很大的麻烦。输出很快就充满了错误。我设法修复了其中的大部分,但我仍然有两个,并且可能是一堆逻辑错误。

我的哈希算法也有问题,所以我用简单的临时代码替换了它。正确的说明是:

The hash function to be used is h(k) = m(k · A mod 1) where A = (√5 − 1)/2 and k · A mod 1 returns the fractional part of k · A.

我想我没有正确实现它。

代码如下:

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

#define TABLE_SIZE 8

typedef struct stItem item;
struct stItem {
    int key;
    item *next;
};

void init(item * H[]) {

    int i = 0;
    for (i; i < TABLE_SIZE; i++)
        H[i] = NULL;
}

int h(int k) {

    // this does not work at all, currently using testcode

    /*
    int m = TABLE_SIZE;
    int A = ( sqrt(5.0) - 1) / 2;

    return m * (k * A % 1);
    */

    return k % TABLE_SIZE;
}

void insert(int key, item * H[]) {

    int keyHashed = h(key);

    if (H[keyHashed] == NULL) {

        item * temp = malloc(sizeof(item));
        temp->key = key;
        temp->next = NULL;

        H[keyHashed] = temp;

        free(temp);
    }

    else {

        item * temp = malloc(sizeof(item));
        temp->next = H[keyHashed]->next;

        while (temp != NULL) {
            temp = temp->next;
        }

        temp->key = key;
        temp->next = NULL;
    }
}

int search(int key, item * H[]) {

        int keyHashed = h(key);

        if (H[keyHashed] == NULL)
            return -1;

        else if (H[keyHashed]->key != key) {

            item * temp = malloc(sizeof(item));
            temp->next = H[keyHashed]->next;

            while (temp->key != key && temp != NULL)
                temp = temp->next;

            if (temp->key == key) {
                free(temp);
                return keyHashed;
            }

            else {
                free(temp);
                return -1;
            }
        }

        else
            return keyHashed;
}

void printHash(item * H[]) {

    printf("Table size: %d", TABLE_SIZE);

    int i = 0;

    for (i; i < TABLE_SIZE; i++) {

        if (H[i] != NULL) {
            printf("i: %d          key: %d",i,H[i]->key);

            if (H[i]->next != NULL) {

                item * temp = malloc(sizeof(item));
                temp->next = H[i]->next;

                while (temp != NULL) {
                    printf(" -> %d", temp->key);
                }

                printf("\n");
            }

            else
                printf("\n");
        }
    }
}

void test() {

    // a)
    int array[7] = {111,10112,1113,5568,63,1342,21231};

    item *h[TABLE_SIZE];
    init(h);

    int i = 0;
    for (i; i < 7; i++)
        insert(array[i], h);


    // b)

    printHash(h);


    // c)

    printf("Search result for 1: %d", search(1, h));
    printf("Search result for 10112: %d", search(10112, h));
    printf("Search result for 1113: %d", search(1113, h));
    printf("Search result for 5568: %d", search(5568, h));
    printf("Search result for 337: %d", search(337, h));
}

int main() {
    test();
}

编辑:多亏了 user3386109 的修复,代码现在编译时没有错误,但发生的是命令提示符只是弹出,其中没有显示任何内容,也没有发生任何事情。它也不关闭。甚至在等待几分钟后也没有。

EDIT2:经过更多测试后,它看起来卡在插入函数上。 test() 中的 for 循环执行后什么都没有。

如果我将此 printf("init done %d", h[1]); 添加到测试函数中的 init() 之后,我得到“init done 0”而不是“init done NULL”,这可能是问题之一吗?

最佳答案

结构定义有误。我会建议以下内容

typedef struct stItem item; 
struct stItem {
    int key;
    item *next;
};

关于c - 链式哈希表的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29882517/

相关文章:

C循环双链表: rev traverse gives different list-pointer address for same node

c++ - 如何编写和调用std::hash? -用于gmp的mpz_class和mpz_t

c - 访问 C 中的结构 - 函数

c - linux内核中的时间戳错误?

c - typedef 一个结构到与该结构同名的点

c - 如果在 fork() 之后使用 execv() ,子进程是否可以访问管道?

c - ARM 汇编 - 添加两个寄存器

hash - 如何破解原像由多个单词组成的SHA-256?

python - 为字典生成唯一标识符?

c# - void 数据类型和 void 指针的实际用途是什么?