【问题标题】:C programming question on the implementation of a hash table关于实现哈希表的C编程问题
【发布时间】:2011-02-20 07:06:12
【问题描述】:

我有一个关于哈希表实现的 C 编程问题。我已经实现了用于存储一些字符串的哈希表。 我在处理哈希冲突时遇到问题。我正在遵循链接链表方法来克服这个问题,但是不知何故,我的代码表现不同。我无法调试它。有人可以帮忙吗?

这就是我所面临的: 说第一次,我插入一个名为 gaur 的字符串。我的哈希图将索引计算为 0 并成功插入字符串。但是,当另一个字符串的哈希值在计算时也为 0 时,我之前的值会被覆盖,即 gaur 将被新字符串替换。

这是我的代码:

struct list
{ 
 char *string;
 struct list *next; 
};

struct hash_table
{
 int size;     /* the size of the table */
 struct list **table; /* the table elements */
}; 

struct hash_table *create_hash_table(int size)
{
    struct hash_table *new_table;
    int i;

    if (size<1) return NULL; /* invalid size for table */

    /* Attempt to allocate memory for the table structure */
    if ((new_table = malloc(sizeof(struct hash_table))) == NULL) {
        return NULL;
    }

    /* Attempt to allocate memory for the table itself */
    if ((new_table->table = malloc(sizeof(struct list *) * size)) == NULL) {
        return NULL;
    }

    /* Initialize the elements of the table */
    for(i=0; i<size; i++) 
     new_table->table[i] = '\0';

    /* Set the table's size */
    new_table->size = size;

    return new_table;
}


unsigned int hash(struct hash_table *hashtable, char *str)
{
    unsigned int hashval = 0;
    int i = 0;

    for(; *str != '\0'; str++)
    {
     hashval += str[i];
     i++;
    }

    return (hashval % hashtable->size);
}

struct list *lookup_string(struct hash_table *hashtable, char *str)
{
    printf("\n enters in lookup_string \n");

    struct list * new_list;
    unsigned int hashval = hash(hashtable, str);

    /* Go to the correct list based on the hash value and see if str is
     * in the list.  If it is, return return a pointer to the list element.
     * If it isn't, the item isn't in the table, so return NULL.
    */


    for(new_list = hashtable->table[hashval]; new_list != NULL;new_list = new_list->next)
    {
        if (strcmp(str, new_list->string) == 0)
          return new_list;
    }
    printf("\n returns NULL in lookup_string \n");
    return NULL;
}


int add_string(struct hash_table *hashtable, char *str)
{
    printf("\n enters in add_string \n");

    struct list *new_list;
    struct list *current_list;
    unsigned int hashval = hash(hashtable, str);
    printf("\n hashval = %d", hashval);

    /* Attempt to allocate memory for list */
    if ((new_list = malloc(sizeof(struct list))) == NULL)
    {
     printf("\n enters here \n");
     return 1;
    }

    /* Does item already exist? */
    current_list = lookup_string(hashtable, str);

    if (current_list == NULL)
    {
     printf("\n DEBUG Purpose \n");
     printf("\n NULL \n");
    }

    /* item already exists, don't insert it again. */
    if (current_list != NULL)
    {
     printf("\n Item already present...\n");
     return 2;
    }

    /* Insert into list */
    printf("\n Inserting...\n");

    new_list->string = strdup(str);
    new_list->next = NULL;
    //new_list->next = hashtable->table[hashval];
    if(hashtable->table[hashval] == NULL)
    {
      hashtable->table[hashval] = new_list;
    }
    else
    {
      struct list * temp_list = hashtable->table[hashval];
      while(temp_list->next!=NULL)
       temp_list = temp_list->next;

      temp_list->next = new_list;
      hashtable->table[hashval] = new_list;
    }

    return 0;
}

【问题讨论】:

  • 我已将您问题中的“怀疑”更改为“问题”。显然,有些语言对英文单词“doubt”和“question”使用相同的词;在这里,“问题”几乎总是你想要的词。
  • 存在new_list的内存泄漏如果/*项已经存在,就不要再插入了。 */ 分支。

标签: c hashtable


【解决方案1】:

我没有检查确认,但这行看起来不对:

hashtable->table[hashval] = new_list;

这是add_string 的最后一个案例的末尾。你有:

  • 正确创建了新的struct list 以保存正在添加的值
  • 正确地找到了该哈希值的链表头,并按自己的方式找到了它的末尾
  • 正确地将新的struct list放在链表的末尾

但是,用我上面引用的那行,你告诉哈希表把 new struct list 放在这个哈希值的链表的 head !从而丢弃了之前存在的整个链表。

我认为你应该省略我上面引用的那一行,看看你的进展如何。前面的行正确地将其附加到现有列表的末尾。

【讨论】:

    【解决方案2】:

    声明hashtable-&gt;table[hashval] = new_list; 是罪魁祸首。您在链接列表的末尾插入了new_list(我认为更好的名称应该是new_node)。但是你用new_list 覆盖这个链表,这只是一个节点。只需删除此声明即可。

    【讨论】:

      【解决方案3】:

      正如其他人已经指出的那样,您正在使用temp_list 走到列表的末尾,将new_list 附加到它上面,然后丢弃现有的列表。

      由于相同的值NULL 用于指示空桶和列表的末尾,因此将新项目放在列表的开头要容易得多。

      您还应该在创建新项目之前进行任何可能导致未添加新项目的测试,否则您会泄漏内存。

      我也会有一个内部查找函数来获取哈希值,否则你必须计算两次

      int add_string(struct hash_table *hashtable, char *str)
      {
          unsigned int hashval = hash(hashtable, str);
      
          /* item already exists, don't insert it again. */
          if (lookup_hashed_string(hashtable, hashval, str))
              return 2;
      
          /* Attempt to allocate memory for list */
          struct list *new_list = malloc(sizeof(struct list));
      
          if (new_list == NULL)
              return 1;
      
          /* Insert into list */
          new_list->string = strdup(str);
          new_list->next = hashtable->table[hashval];
      
          hashtable->table[hashval] = new_list;
      
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        散列函数必须是一个函数,它将您的数据输入并返回分隔的 id(例如:0 到 HASH_MAX 之间的整数)

        然后您必须将您的元素存储在hash_table 数组的Hash(data) 索引中的列表中。如果一个数据具有相同的哈希值,它将与之前的数据在同一个列表中。

        struct your_type_list {
          yourtype data;
          yourtype *next_data;
        };
        
        struct your_type_list hash_table[HASH_MAX];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-01-19
          • 2011-01-30
          • 1970-01-01
          • 1970-01-01
          • 2010-10-07
          • 2012-05-27
          相关资源
          最近更新 更多