gpt4 book ai didi

c - 如何调用这个函数来进行字符串查找,然后将结果打印到屏幕上?

转载 作者:行者123 更新时间:2023-11-30 21:10:04 25 4
gpt4 key购买 nike

下面列出了该函数。我想知道如何编写用户代码来查找字符串并将字符串打印到屏幕上。

list_t *lookup_string(hash_table *hashtable, char *str) {

list_t *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
// a pointer to the list element. If it isn't, the item isn't in the table, so return NULL.

for (list = hashtable->table[hashval]; list != NULL; list = list->next) {

if (strcmp(str, list->next) == 0) {

return list;

}

return NULL;
}
}

下面列出了用户定义的数据类型。

typedef struct _list_t_ {

char *string;
struct _list_t_ *next;

} list_t;

typedef struct _hash_table_t_ {

int size; //the size of the table
list_t **table; //the table elements

} hash_table;

最佳答案

您不应将字符串与 list->next 进行比较,而应将其与哈希表条目的键进行比较。

for (list = hashtable->table[hashval]; list != NULL; list = list->next) {
if (strcmp(str, list->string) == 0) {
return list;
}
return NULL;
}

关于c - 如何调用这个函数来进行字符串查找,然后将结果打印到屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32406557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com