【问题标题】:Adding items into my hash table in C在 C 中将项目添加到我的哈希表中
【发布时间】:2011-06-21 02:06:48
【问题描述】:

我正在尝试将一个项目添加到我的 Hashtable 中,我已经使用了很多 printf 来看看发生了什么,但它看起来应该添加它,但实际上不是。

这是我的代码:

struct hashnode_s {
    char *key;
    ValueType tag;
    union
    {
        int IntegerValue;
        char *StringValue;
    }u;
    struct hashnode_s *next;
};

我正在尝试模仿 GCC 编译器。 用我的哈希表

typedef struct hashtbl {
    hash_size size;
    struct hashnode_s **nodes;
    hash_size (*hashfunc)(const char *);
} HASHTBL;

和我的插入方法

int hashtbl_InsertString(HASHTBL *hashtbl, const char *key, const char *value)
{
    struct hashnode_s *node;
    hash_size hash;

    hash = SearchForHashIndex(hashtbl, key, value);
    if(hash ==-1)
    {
        hash=hashtbl->hashfunc(key);
    }

    fprintf(stderr, "hashtbl_insert() key=%s, hash=%d\n\n\n", key, hash);

    node=hashtbl->nodes[hash];
    while(node)
    {   
        printf("In while\n\n\n\n\n");   
        /* This Code isn't correct 
        if(!strcmp(node->key, key)) {
            node->data=data;
            return 0;
        }*/
        node=node->next;
    }

    if(!(node=malloc(sizeof(struct hashnode_s)))) return -1;
    if(!(node->key=mystrdup(key))) {
        free(node);
        return -1;
    }
    node->key = key;
    node->tag = StringConst;
    node->u.StringValue = value;

    node->next=hashtbl->nodes[hash];    

    printf("ADDING HASH NODE \n\n\n");

    hashtbl->nodes[hash]=node;

    return 0;
}

我在搜索方法时不断得到空值。不应该是这样。我是否正确插入?

int SearchForHashIndex(HASHTBL *hashtbl, const char *key, const char *value)
{
    printf("INSIDE SEARCH FOR HASH INDEX \n\n\n\n\n");
    int i;

    for(i=0; i < CurrentHashSize; i++)
    {   
        struct hashnode_s *node;    
        node = hashtbl->nodes[i];
        printf("%d\n",i);
        if(node == NULL)
        {
            printf("NULL");
        }
        while(node)
        {
            if(strcmp(node->key,key) || strcmp(node->u.StringValue,value))
            {
                printf("INSIDE HERE!\n");
                return i;
                printf("returning %d\n",i);
            }
            node = node->next;
        }
    }
    printf("returning -1\n");
    return -1;
}

【问题讨论】:

  • 首先,你的printf("returning %d\n", i) 永远不会计算,因为它在return 语句之后。
  • 是的,我注意到了。那是我在那里的东西,我忘了删除大声笑
  • 代码的格式是缩进四个空格(也有一个按钮),而不是使用
     标签。

标签: c hashtable


【解决方案1】:

strcmp() 在成功比较时返回 0。您需要在搜索功能中更改您的 if() 条件。

此外,插入例程中的这个块:

node=hashtbl->nodes[hash];
while(node)
{   
    printf("In while\n\n\n\n\n");   
    /* This Code isn't correct 
    if(!strcmp(node->key, key)) {
        node->data=data;
        return 0;
    }*/
    node=node->next;

}

没用,因为你只是在之后直接将新分配的内存分配给节点。

【讨论】:

  • 我认为该块是指当 OP 可以让“添加节点”部分工作时,检查节点是否已经添加,并且可能会在分配新节点之前返回完成后。
【解决方案2】:

这看起来不对:

if(!(node->key=mystrdup(key))) {
    free(node);
    return -1;
}
node->key = key;

您将node-&gt;key 设置为(显然)提供的key 的副本;然后您立即用函数参数覆盖该指针,这不太可能是正确的。

【讨论】:

  • 我试图通过添加 myu 第一个节点。哈哈,我什至无法理解。一旦我明白了,那么我可以在下一个节点上添加更多节点,以便忽略该部分
  • @Kevin:在我看来,这段代码将针对第一个节点执行。
  • @Kevin - @caf 所指的部分不是注释掉的部分。 node-&gt;key = key 覆盖之前的 node-&gt;key 值,这是您使用 mystrdup 分配的内存。您正在丢弃分配的内存(这是泄漏)并用用户传递给函数的任何参数替换它。
  • 我迟到了!!每次我找到特定的#define 叙述时,我都会继续重新创建我的表格!!!!我忘了重置我的布尔值!!
【解决方案3】:

如果您的散列函数是可靠的(并且它必须是一个散列函数),那么您的搜索函数就不必遍历所有链表。您不需要那个外部 for 循环 - 只需将 i 设置为 hashtabl-&gt;hashfunc(key) 并在该列表中搜索该元素。如果它不在那个列表中,它不应该在任何列表中,如果是,那么你的插入函数肯定是错误的。

事实上,您的搜索函数应该返回 hashtabl-&gt;hashfunc(key) 或 -1。

另外,如果有人对不同的对象使用相同的密钥会发生什么?

【讨论】:

  • 这是我第一次搞乱哈希表。我还在挣扎哈哈。试图理解,我通常不会用 C 编程。我更像是 wpf/c#
猜你喜欢
  • 2021-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多