2015-09-04 57 views
-3

該功能列在下面。我想知道如何編寫用戶代碼來查找字符串並將字符串打印到屏幕上。如何調用此函數執行字符串查找,然後將結果輸出到屏幕?

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; 
+3

嗯...是不是兩個相同的片段?無論如何,這個問題還不清楚。 –

+3

即使沒有結構定義,「strcmp(str,list-> next)」也很可疑。 – EOF

+1

...,如果懷疑「next」的確是'list_t *',應該爲不兼容的類型發出警告或錯誤。 – WhozCraig

回答

0

你不應該list->next字符串比較,你應該用哈希表項的鍵進行比較。

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

你是對的Barmar。你的建議解決了這個問題。 –

相關問題