Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C编程中关于哈希表实现的问题_C_Hashtable - Fatal编程技术网

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

C编程中关于哈希表实现的问题,c,hashtable,C,Hashtable,我有一个关于哈希表实现的C编程问题。我已经实现了用于存储一些字符串的哈希表。 我在和儿子打交道时遇到了问题。我采用链表的方法来解决这个问题,但不知何故,我的代码表现出了不同的行为。我无法调试它。有人能帮忙吗 这就是我面临的问题: 第一次,我插入了一个名为gaur的字符串。我的哈希映射将索引计算为0并成功插入字符串。但是,当另一个字符串的哈希值(在计算时)也为0时,我以前的值被重写,即gaur将被新字符串替换 这是我的代码: struct list { char *string; struc

我有一个关于哈希表实现的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;
}
struct列表
{ 
字符*字符串;
结构列表*下一步;
};
结构哈希表
{
int size;/*表的大小*/
结构列表**表;/*表元素*/
}; 
结构哈希表*创建哈希表(整数大小)
{
结构哈希表*新哈希表;
int i;
if(sizetable=malloc(sizeof(struct list*)*size))==NULL){
返回NULL;
}
/*初始化表的元素*/
对于(i=0;itable[i]='\0';
/*设置桌子的大小*/
新建表格->大小=大小;
返回新的_表;
}
无符号整数哈希(结构哈希表*hashtable,char*str)
{
无符号整数hashval=0;
int i=0;
对于(;*str!='\0';str++)
{
hashval+=str[i];
i++;
}
返回值(hashval%hashtable->size);
}
结构列表*查找字符串(结构哈希表*哈希表,字符*str)
{
printf(“\n进入查找\u字符串\n”);
结构列表*新列表;
unsigned int hashval=hash(哈希表,str);
/*根据哈希值转到正确的列表,查看str是否正确
*如果是,则返回指向列表元素的指针。
*如果不是,则该项不在表中,因此返回NULL。
*/
for(新建列表=哈希表->表[hashval];新建列表!=NULL;新建列表=新建列表->下一步)
{
如果(strcmp(str,新列表->字符串)=0)
返回新的_列表;
}
printf(“\n在查找字符串中返回NULL\n”);
返回NULL;
}
int add_字符串(结构哈希表*哈希表,字符*str)
{
printf(“\n输入add_字符串\n”);
结构列表*新列表;
结构列表*当前_列表;
unsigned int hashval=hash(哈希表,str);
printf(“\n hashval=%d”,hashval);
/*尝试为列表分配内存*/
if((new_list=malloc(sizeof(struct list)))==NULL)
{
printf(“\n在此输入\n”);
返回1;
}
/*项目是否已经存在*/
当前列表=查找字符串(哈希表,str);
if(当前_列表==NULL)
{
printf(“\n调试目的\n”);
printf(“\n NULL\n”);
}
/*项已存在,请不要再次插入它*/
if(当前_列表!=NULL)
{
printf(“\n项已存在…\n”);
返回2;
}
/*插入列表*/
printf(“\n插入…\n”);
新建列表->字符串=strdup(str);
新建列表->下一步=空;
//新建列表->下一步=哈希表->表[hashval];
if(hashtable->table[hashval]==NULL)
{
hashtable->table[hashval]=新建_列表;
}
其他的
{
结构列表*temp_list=hashtable->table[hashval];
while(临时列表->下一步!=NULL)
临时列表=临时列表->下一步;
临时列表->下一步=新列表;
hashtable->table[hashval]=新建_列表;
}
返回0;
}

我还没有检查确认,但这行看起来有误:

hashtable->table[hashval] = new_list;
这是
添加字符串的最后一个案例的结尾。您有:

  • 正确创建了新的
    结构列表
    ,以保存所添加的值
  • 正确地找到了该hashvalue的链接列表的头,并一直工作到最后
  • 正确地将新的
    结构列表
    放在链接列表的末尾
但是,在我上面引用的这一行中,您告诉哈希表将新的
结构列表
放在这个hashvalue的链接列表的,从而丢弃了以前的整个链接列表


我想你应该省略上面我引用的那一行,看看你是怎么做的。前面的几行正确地将它附加到现有列表的末尾。

哈希函数必须是一个函数,它在条目中获取数据并返回分隔id(例如:0和hash_MAX之间的整数)

然后必须将元素存储在
哈希表
数组的
哈希(数据)
索引中的列表中。如果数据具有相同的哈希,则它将与以前的数据存储在相同的列表中

struct your_type_list {
  yourtype data;
  yourtype *next_data;
};

struct your_type_list hash_table[HASH_MAX];

语句
hashtable->table[hashval]=new\u list;
是罪魁祸首。您插入了
new\u list
(我认为更好的名称应该是
new\u node
)在链接列表的末尾。但是,然后您将使用仅为单个节点的
new\u list
覆盖此链接列表。只需删除此语句。

正如其他人已经指出的,您将使用
temp\u list
走到列表的末尾,将
new\u 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;
}

我已将您问题中的“怀疑”改为“问题”。显然,有些语言的英语单词“怀疑”和“问题”使用相同的单词;因此,“问题”几乎总是您想要的单词。新列表中存在内存泄漏
如果/*项已存在,请不要再插入它。*/。