C编程问题关于哈希表的实现

aks*_*aks 2 c hashtable

我有一个关于哈希表实现的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;
}
Run Code Online (Sandbox Code Playgroud)

Aak*_*shM 5

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

hashtable->table[hashval] = new_list;
Run Code Online (Sandbox Code Playgroud)

这是在最后一个案例的结尾add_string.你有:

  • 正确创建新的struct list以保存正在添加的值
  • 正确地找到了该hashvalue的链表的头部,并按照你的方式工作到最后
  • 正确地将新的struct list放在链表的末尾

然后,使用上面引用的那一行,你告诉哈希表将new struct list放在这个hashvalue的链表的头部!因此丢掉之前存在的整个链表.

我想你应该省略我上面引用的那一行,看看你是如何继续的.前面的行正确地将它附加到现有列表的末尾.