雜湊(雜湊) (四)C語言實現 雜湊 開雜湊法

2021-08-28 17:36:41 字數 3688 閱讀 4608

雜湊(雜湊)的概念:

c語言實現:靜態雜湊表:

c語言實現:動態雜湊表

要想達到乙個好的閉雜湊減小雜湊衝突的目的;就對我們的的雜湊函式的選擇提出了要求:

常見的雜湊函式有:

直接定製法 :

設雜湊表中允許的位址數為m,取乙個不大於m,但接近或者等於m的質數p作為除數,按照雜湊函式:hash(key) = key % p(p<=m),將關鍵碼轉換成雜湊位址  

平方取中法 :

假設關鍵字為1234,對它平方就是1522756,抽取中間的3位227作為雜湊位址;   再比如關鍵字為4321,對它平方就是18671041,抽取中間的3位671(或710)作為雜湊位址   平方取中法比較適合:不知道關鍵字的分布,而位數又不是很大的情況  

摺疊法 :

摺疊法是將關鍵字從左到右分割成位數相等的幾部分(後一部分位數可以短些),然後將這幾部分疊加求和,並按雜湊表表 長,取後幾位作為雜湊位址   摺疊法適合事先不需要知道關鍵字的分布,適合關鍵字位數比較多的情況  

隨機數法 :

選擇乙個隨機函式,取關鍵字的隨機函式值為它的雜湊位址,即h(key) = random(key),其中random為隨機數函式   通常應用於關鍵字長度不等時採用此法  

具體情形如圖:

底層構造:

#pragma once

typedef int datatype;

typedef struct hashnode

hashnode;

typedef struct hashbucket

hashbucket;

void hashbucketinit(hashbucket *ht,int capacity);

void hashbucketinsertunique(hashbucket* ht,datatype data); //只能插入不同元素

void hashbucketinsertequal(hashbucket* ht,datatype data); //可以插入相同元素

void hashbucketdelereunique(hashbucket* ht,datatype data); //相同元素只刪除乙個

void hashbucketdelereequal(hashbucket* ht, datatype data); //刪除所有相同元素

int hashbucketsize(hashbucket *ht);

int hashbucketempty(hashbucket *ht);

void hashbucketdestroy(hashbucket *ht);

void printfhashbucket(hashbucket* ht);

具體操作:

#include#include#include#includeint hashfunc(hashbucket *ht,datatype data)           //獲得雜湊位址(除留餘數法)

hashnode* buyhashnode(datatype data) //獲得節點(插入時使用)

pnewnode->_data = data;

pnewnode->_pnext = null;

return pnewnode;

}void hashbucketinit(hashbucket *ht,int capacity) //初始化

for (; i <=capacity; i++)

ht->_table[i]=null;

ht->_capacity = capacity;

ht->_size = 0;

}void hashbucketinsertunique(hashbucket* ht, datatype data)

//建立新節點

pnewnode = buyhashnode(data);

//頭插

pnewnode->_pnext = ht->_table[bucketno];

ht->_table[bucketno] = pnewnode;

ht->_size++;

}void hashbucketinsertequal(hashbucket* ht, datatype data)

void hashbucketdelereunique(hashbucket* ht, datatype data)

else //不是首元素

free(pcur);

ht->_size--;

return;

} ppre = pcur;

pcur = pcur->_pnext; }}

void hashbucketdelereequal(hashbucket* ht, datatype data)

else //不是首元素

}ppre = pcur;

pcur = pcur->_pnext; }}

int hashbucketsize(hashbucket *ht)

int hashbucketempty(hashbucket *ht)

void hashbucketdestroy(hashbucket *ht) }

free(ht->_table);

ht->_capacity = 0;

ht->_size = 0;

}void printfhashbucket(hashbucket* ht)

printf("null\n"); }

}

進行測試

新建原始檔:hash.c

#include"hashbucket.h"

int main()

執行結果

第0號桶:  null

第1號桶: null

第2號桶: 12--->2--->null

第3號桶: 13--->3--->null

第4號桶: null

第5號桶: 25--->15--->5--->null

第6號桶: null

第7號桶: null

第8號桶: 28--->18--->8--->null

第9號桶: null

有效元素個數為:10

第0號桶: null

第1號桶: null

第2號桶: 12--->2--->null

第3號桶: 13--->3--->null

第4號桶: null

第5號桶: 25--->null

第6號桶: null

第7號桶: null

第8號桶: 18--->8--->null

第9號桶: null

有效元素個數為:7

請按任意鍵繼續. . .

雜湊錶開雜湊雜湊桶實現

開雜湊法對關鍵碼集合用雜湊函式計算雜湊位址,具有相同位址的關鍵碼歸於同一子集合,每乙個子集合稱為乙個桶,各個桶中的元素通過乙個單鏈表鏈結起來,各鍊錶的頭結點組成 乙個向量,因此,向量的元素個數與可能的桶數一致。include using namespace std namespace openhas...

C 開雜湊雜湊表(雜湊桶)

開雜湊概念 開雜湊法又叫鏈位址法 開鏈法 首先對關鍵碼集合用雜湊函式計算雜湊位址,具有相同位址的關鍵碼歸於同一子集合,每乙個子集合稱為乙個桶,各個桶中的元素通過乙個單鏈表連線起來,各鍊錶的頭結點儲存在雜湊表中。開雜湊中每個桶中放的都是發生雜湊衝突的元素。節點定義 template class v s...

雜湊之開雜湊,閉雜湊

有沒有一種方法時間複雜度,僅僅o 1 尼,那麼我們就要構造一種儲存結構,通過某種函式是之元素與它對應的關鍵碼之間能建立一一對映的關係,那麼自然通過之中一一對映的關係,我們就可以很快的找到我們需要的元素。所以進入雜湊這個這題首先我們需要乙個我們下標,這個下表在雜湊當中 我們就稱之為雜湊位址。而這個位址...