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

标签 c data-structures hashtable

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

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/

相关文章:

c - for循环函数进入时崩溃

子进程的 printf 未显示在父进程的控制台中

c++ - 残差图的最快数据结构

javascript - 如何通过在 python 的所有输出中添加 name1.name 2.... 来修改输出

c - C 中的快速布隆过滤器 - 64 位整数,高频初始化/查询/销毁循环

c - RGB 到 RGB 加琥珀色

java - 我的 DFS 图形方法不会从 int 变为 String

java - 如何关联JTextArea和Object?如何知道哪个对象属于哪个jtextArea?

c++ - 如何最好地在 C++ 中为哈希表创建链式方法?

c - 指向数组结构的指针