雜湊衝突的兩種解決辦法

2021-08-26 09:20:14 字數 2981 閱讀 8674

1.開雜湊(開放位址法)

缺點:所有衝突連在一起易產生資料堆積,占用別的資料的關鍵碼,導致搜尋速率下降

#pragma once

#include #include #include typedef int keydatatype;

typedef int valuedatatype;

typedef enum state

state;

typedef struct hashdata

hashdata;

typedef struct hashtable

hashtable;

void hashtableinit(hashtable* ht,size_t capacity);

hashdata* hashtablefind(hashtable* ht, keydatatype key);

int hashtableinsert(hashtable* ht, keydatatype key, valuedatatype value);

int hashtableremove(hashtable* ht, keydatatype key);

void hashtabledestory(hashtable* ht);

void printhashtable(hashtable* ht);

#include "hashtable.h"

int hashfunc(hashtable* ht, keydatatype key)

void hashtableinit(hashtable* ht, size_t capacity)

}int hashtableinsert(hashtable* ht, keydatatype key, valuedatatype value)

int pos = hashfunc(ht, key);

while (ht->_tables[pos].state == exits)

ht->_tables[pos].key = key;

ht->_tables[pos].value = value;

ht->_tables[pos].state = exits;

ht->size++;

return 1;

}hashdata* hashtablefind(hashtable* ht, keydatatype key)

else

}printf("沒找到!\n");

return null;

}int hashtableremove(hashtable* ht, keydatatype key)

return 0;

}void hashtabledestory(hashtable* ht)

void printhashtable(hashtable* ht)

}}

2.開雜湊(拉鍊法)

思想:將具有相同關鍵碼的元素用單鏈表連線起來,每乙個鏈稱為乙個桶

每個單鏈表的頭節點存在雜湊表中。不存在溢位

#pragma once

#include #include #include typedef int keydata;

typedef int valuedata;

typedef enum state

state;

typedef struct hashdata

hashdata;

typedef struct hashtable

hashtable;

void hashtableinit(hashtable* ht, size_t capacity);

hashdata* hashtablefind(hashtable* ht, keydata key);

int hashtableinsert(hashtable* ht, keydata key, valuedata value);

int hashtableremove(hashtable* ht, keydata key);

void hashtabledestory(hashtable* ht);

void printhashtable(hashtable* ht);

#include "hashtable.h"

size_t hashfunc(hashtable* ht, keydata key) //計算位置

hashdata* buynode(keydata key, valuedata value)

void hashtableinit(hashtable* ht, size_t capacity)

}hashdata* hashtablefind(hashtable* ht, keydata key)

cur = cur->_next;

} printf("沒找到!\n");

return null;

}int hashtableinsert(hashtable* ht, keydata key, valuedata value)

else

cur->_next = buynode(key, value); //掛到桶最後

return 1; }}

int hashtableremove(hashtable* ht, keydata key)

if (cur->_next) //刪除桶的非尾節點

else //刪除桶的尾節點 }

void hashtabledestory(hashtable* ht)

} }free(ht->_data); //銷毀雜湊表

ht->_data = null;

ht->capacity = 0;

}void printhashtable(hashtable* ht)

} }}

雜湊儲存和衝突解決辦法

一 雜湊表的概念及作用 一般的線性表,樹中,記錄在結構中的相對位置是隨機的,即和記錄的關鍵字之間不存在確定的關係,因此,在 結構中查詢記錄時需進行一系列和關鍵字的比 較。這一類查詢方法建立在 比較 的基礎上,查詢的 效率依賴於查詢過 程中所進行的比較次數。理想的情況是能直接找到需要的記錄,因此必須在...

Oracle 兩種報錯的解決辦法

其實這兩個問題的原因是一樣的,都是因為表空間不足,需要檢查表空間是否是自增的,如果不是,需要修改為自增的表空間。如果檢查發現表空間已經是自增的,但是還報這樣的錯誤,那麼即需要看看表空間所用的資料檔案儲存的磁碟格式是什麼格式,如果你是在windows測試機器上,並且系統磁碟格式是fat32,那麼這就是...

解決雜湊衝突的四種辦法

雜湊是通過對資料進行再壓縮,提高效率的一種解決方法。但由於通過雜湊函式產生的雜湊值是有限的,而資料可能比較多,導致經過雜湊函式處理後仍然有不同的資料對應相同的雜湊值。這時候就產生了雜湊衝突。裝填因子 裝填因子 資料總數 雜湊表長 雜湊函式 處理衝突的方法 解決雜湊衝突的四種方法 1 線性探測 按順序...