C-如何在哈希图中为同一个键分配多个值?

时间:2018-11-11 12:14:09

标签: c algorithm pointers data-structures linked-list

作为一名新手程序员,我开始阅读The C Programming Language,以了解有关pointersstructs的更多信息。

我目前正在学习C语言中的哈希表。按照本书中的示例,我创建了自己的哈希表来保存键值对:

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

#define MAX_STRING_SIZE 100
#define HASHSIZE 101

static struct map *hashtab[HASHSIZE]; /* pointer table */
unsigned hash(char *);  /* hashing function: form hash value for string s. used by both lookup and install*/
char *strdup(char *);

struct map {        /* creating a map structure */
    struct map *next;
    char *KEY;          /* KEY - pointer to a char - member of nlist*/
    char *object1;      /* object - pointer to a char - member of nlist*/
};

/* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
struct map *lookup(char *s) {
    struct map *np;

    for (np = hashtab[hash(s)]; np != NULL; np = np->next) {
        if (strcmp(s, np->KEY) == 0) {
            return np;
        }
    }
    return NULL;
}

/* install function takes a pointer to a char - KEY, object and returns a pointer to map structure */
struct map *install(char *KEY, char *object1) {
    /* install: put (name, defn) in */
    /* Install uses lookup to determine whether the KEY being installed
       is already present. Proceeds to create a new entry or update*/
    struct map *np;
    unsigned hashval;

    if ((np = lookup(KEY)) == NULL) {
        np = (struct map *) malloc(sizeof(*np));
        if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
            return NULL;
        }
        hashval = hash(KEY);
        np->next = hashtab[hashval];
        hashtab[hashval] = np;
    }
    else {
        free((void *)np->object1);
    }
    if ((np->object1 = strdup(object1)) == NULL) {
        return NULL;
    }
    return np;
}

然后我将值分配给键,如下所示:

int main(void) {

    struct map *table[4] = {
        (install("key1", "value1")),
        (install("key2", "value2")),
        (install("key3", "value3")),
        (install("key4", "value4"))
    };

    int i;

    for (i = 0; i < 4; i++) {
        printf("%s->%s\n", table[i]->KEY, table[i]->object);
    }
    printf("\n");
    return 0;
}

上面的代码运行良好,我能够分别将value1,...,value4分配给key1,...,key4。但是,这不允许我将多个值分配给同一键。

假设我从一个文本文件中读取了以下内容:

key1 9000 600 Test1
key2 2000 600 Test2
key3 3000 120 Test3
key4 4000 120 Test4
.
.
key10 1000 560 Test10

我希望能够存储每个键并为其分配多个值。由于列数是固定的,所以也许我可以有一个代表行的结构并将其放入地图中。

为此,需要修改install以便能够为同一键添加多个值。由于键是唯一的,因此lookupuninstall(我创建的用于删除键的功能)应保持不变。

上面的代码可以完美地工作,但是我正在寻找将多个值添加到同一键的一般解决方案。

我应该如何继续打电话:

struct map *table[4] = {
    (install("key1", "9000" ,"600", "Test1")),   //key, object1, object2, object3
    (install("key2", "2000" ,"600", "Test2")),
    (install("key3", "3000" ,"120", "Test3")),
    (install("key4", "4000" ,"120", "Test4"))
};

另一个想法是:

/* key points to this struct */
struct Value {
    int i;
    int k;
    char *c;
};

typedef struct map {        /* creating a map structure */
    struct map *next;
    char *KEY;
    struct Value value;  /* place multiple values inside a struct*/
};

这就是我被困住的地方:

struct map *insert(char *KEY, struct *Value) {
    struct map *np;
    unsigned hashval;

    if ((np = lookup(KEY)) == NULL) {
        np = (struct map *) malloc(sizeof(*np));
        if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
            return NULL;
        }
        hashval = hash(KEY);
        np->next = hashtab[hashval];
        hashtab[hashval] = np;
    }
    else {
        free((struct Value*)np->value); //type cast cannot convert from 'Value' to 'Value*'

    }
    if ((np->Value = strdup(Value)) == NULL) { //map has no field value
        return NULL;
    }

    return np;
}

我搜索了以下问题,但是无法提取有关如何在C语言中实现此问题的相关信息。

Hashmaps having multiple keys with multiple values

How can I assign multiple values to a hash key?

HashMap with multiple valued keys

如何实现一个哈希映射,该哈希映射接受分配给同一键的多个值?


编辑

正如评论中指出的那样,我使用的结构实际上可能不是哈希图,而是链表。但是book特别指出第144页是哈希图。

1 个答案:

答案 0 :(得分:0)

这可能不是最好的解决方案,但是由于上面的评论,我有了一些帮助。我声明多个值作为指向char的指针。请让我知道我是否可以改善这一点。

我尚未实施碰撞校正。

struct map {        /* creating a map structure */
    struct map *next;
    char *KEY;
    char *value1;
    char *value2;
    char *value3;
}; 

然后将它们传递给插入函数。

struct map *insert(char *KEY, char *value1, char *value2, char *value3) {
    /* install: put (name, defn) in */
    /* Install uses lookup to determine whether the KEY being installed
       is already present. Proceeds to create a new entry or update*/
    struct map *np;
    unsigned hashval;

    if ((np = lookup(KEY)) == NULL) {
        np = (struct map *) malloc(sizeof(*np));
        if (np == NULL || (np->KEY = strdup(KEY)) == NULL) {
            return NULL;
        }
        hashval = hash(KEY);
        np->next = hashtab[hashval];
        hashtab[hashval] = np;
    }
    else {
        free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
        free((void *)np->value2);
        free((void *)np->value3);
    }
    if ((np->time = strdup(value1)) == NULL) { //map has no field value
        return NULL;
    }
    if ((np->description = strdup(value2)) == NULL) { //map has no field value
        return NULL;
    }
    if ((np->duration = strdup(value3)) == NULL) { //map has no field value
        return NULL;
    }

    return np;
}

打印这些将给出指向四个值的键。

key1->value1 value2 value3