2013-06-24 20 views
0

我是C的新手,並且我有一個需要在「電子郵件ID或ipv4/6地址」的基礎上進行快速查找的要求在C中的大小爲1K的列表中進行散列實施

我struture應該像

{ 
    enum_usr_type ;/*(which can be either e-mail type or ip-address type)*/ 

    char_id_data ; /*(which can be either a email-id string OR ipv4/6 ip-address string) */ 
} 

完整的數據庫的規模預計爲1K。

任何人都可以提出任何指示我應該如何去做(也許散列表,但我不熟悉)。

+0

通過連續的數組線性搜索。 –

+0

是的,在這種情況下,散列表是一個很好的解決方案。你有什麼具體的要求/問題嗎? – 2013-06-24 10:41:45

+0

@KerrekSB的確,1000個條目並不是很多數據。 – 2013-06-24 10:42:11

回答

0

POSIX哈希表管理

man hsearch 

Libhash

ftp://ftp.ugh.net.au/pub/unix/libhash/

Libhash是用C寫的小散庫

一旦安裝

man libhash 

我個人比較喜歡libhash,這是非常小的,你可以嵌入在你的代碼很容易,也很非常容易使用:

hash h; 

/* in your case number_of_buckets = 2053, 1k*2 = 2048, the next prime number is 2053. */ 
hash_initialise(&h, 2053U, NULL, NULL, NULL, free, free) 

/* insert */ 
hash_insert(&h, key, value); 

/* retrieve */ 
hash_retrieve(&h, key, &ptr); 

/* dispose hash table */ 
hash_deinitialise(&h); 
再次

man libhash 
+0

我四處張望,在某些地方DJB2算法推薦使用字符串哈希...你能告訴我如何計算DJB2的桶大小 –

+0

@HimanshuGupta它取決於你使用的哈希函數的性質,例如,一些如果它是桶的素數,則表現得更好,對於其他人可能並不重要,在這種情況下,如果是素數,則使用libhash更好。在你的情況下,JB2只是哈希函數(如果我錯了,有人糾正我),你仍然需要處理碰撞和更多的事情,最終找到你正在尋找的東西。就像n.m所說的,用1Kb的數據不要開始思考快速,簡單去吧。 – yeyo

+0

@HimanshuGupta如果你想實現你自己的哈希表,不要這樣做,你會重新發明輪子。 – yeyo