如何使用 C 打印 hashset 的所有值?我已经将字符串存储在哈希集中,现在我想显示它
How to print all the values of hashset using C? I have stored the string in the hashset now I want to display it
hashset *newset= new_hashset;
char *info="1234";
put_hashset (newset,info);
我得到了 here 的帮助。我已在哈希集中插入值,但无法打印这些值。
从header file来看,API不包含那种操作。你所能做的就是检查一个字符串是否在集合中,不支持迭代所有添加的字符串。
你可以这样做:
hashset *newset = new_hashset(); /* You need this! */
char * const info = "1234";
put_hashset(newset, info);
printf("%s\n", has_hashset(newset, "1234") ? "yes" : "no");
它应该打印 yes
.
当然你可以添加对迭代的支持,但这有点超出了这个问题的范围。这并不难,但您需要了解哈希集的设计。
这是一个标准的哈希表结构。一个大小,加上一个链表数组。所以你需要遍历数组,然后遍历列表(大多数列表很短,有些是空的)。数据排名不分先后
void getdata(hashset *hash)
{
int i;
hashnode *ptr;
for(i=0;i<hash->size;i++)
{
if(hash->chains[i])
{
for(ptr = hash->chains[i]; ptr; ptr = ptr->link)
{
printf("key : %s\n", ptr->word);
}
}
}
}
hashset *newset= new_hashset;
char *info="1234";
put_hashset (newset,info);
我得到了 here 的帮助。我已在哈希集中插入值,但无法打印这些值。
从header file来看,API不包含那种操作。你所能做的就是检查一个字符串是否在集合中,不支持迭代所有添加的字符串。
你可以这样做:
hashset *newset = new_hashset(); /* You need this! */
char * const info = "1234";
put_hashset(newset, info);
printf("%s\n", has_hashset(newset, "1234") ? "yes" : "no");
它应该打印 yes
.
当然你可以添加对迭代的支持,但这有点超出了这个问题的范围。这并不难,但您需要了解哈希集的设计。
这是一个标准的哈希表结构。一个大小,加上一个链表数组。所以你需要遍历数组,然后遍历列表(大多数列表很短,有些是空的)。数据排名不分先后
void getdata(hashset *hash)
{
int i;
hashnode *ptr;
for(i=0;i<hash->size;i++)
{
if(hash->chains[i])
{
for(ptr = hash->chains[i]; ptr; ptr = ptr->link)
{
printf("key : %s\n", ptr->word);
}
}
}
}