该功能如下所列。我想知道如何编写用户代码来查找字符串并将字符串打印到屏幕上。
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;
答案 0 :(得分:0)
您不应该将字符串与list->next
进行比较,您应该将其与哈希表条目的键进行比较。
for (list = hashtable->table[hashval]; list != NULL; list = list->next) {
if (strcmp(str, list->string) == 0) {
return list;
}
return NULL;
}