#include<malloc.h>
#include<assert.h>
#include<string.h>
typedef int KeyType;
typedef int ValueType;
typedef struct HashNode
{
KeyType _key;
ValueType _value;
struct HashNode* _next;
}HashNode;
typedef struct HashTable
{
HashNode** _tables;
size_t _size;
size_t _N;
}HashTable;
HashNode* BuyHashNode(KeyType key, ValueType value);//创建一个节点
size_t HashFunc(KeyType key, size_t N);//查找哈希位置
size_t Get_Next_Prime_Num(size_t cur_num);//素数表
void Check_Capacity(HashTable* ht);//检查容量
void HashTableInit(HashTable* ht);//创建哈希表
int HashTableInsert(HashTable* ht, KeyType key, ValueType value);//插入
HashNode* HashTableFind(HashTable* ht, KeyType key);//查找
int HashTableRemove(HashTable* ht, KeyType key);//删除
void HashTableDestory(HashTable* ht);//销毁表
HashNode* BuyHashNode(KeyType key, ValueType value)
{
HashNode* node = (HashNode*)malloc(sizeof(HashNode));
assert(node);
node->_key = key;
node->_value = value;
node->_next = NULL;
return node;
}
size_t HashFunc(KeyType key, size_t N)
{
return key % N;
}
size_t Get_Next_Prime_Num(size_t cur_num)//素数表
{
const int _PrimeSize = 28;
static const unsigned long _PrimeList[_PrimeSize] = {
53ul, 97ul, 193ul, 389ul, 769ul,
1543ul, 3079ul, 6151ul, 12289ul, 24593ul,
49157ul, 98317ul, 196613ul, 393241ul, 786433ul,
1572869ul, 3145739ul, 6291469ul, 12582917ul, 25165843ul,
50331653ul, 100663319ul, 201326611ul, 402653189ul, 805306457ul,
1610612741ul, 3221225473ul, 4294967291ul
};
for (size_t i = 0; i < _PrimeSize; i++)
{
if (cur_num < _PrimeList[i])
{
return _PrimeList[i];
}
}
return _PrimeList[_PrimeSize - 1];
}
void HashTableInit(HashTable* ht)
{
ht->_tables = (HashNode**)malloc(sizeof(HashNode*) * Get_Next_Prime_Num(0));
assert(ht->_N);
ht->_N = Get_Next_Prime_Num(0);
ht->_size = 0;
memset(ht->_tables, NULL, sizeof(HashNode*) * ht->_N);
}
void Check_Capacity(HashTable* ht)
{
if (ht->_size > ht->_N)
{
HashTable newht;
newht._tables = (HashNode**)malloc(sizeof(HashNode*)*Get_Next_Prime_Num(ht->_N));
newht._N = Get_Next_Prime_Num(ht->_N);
memset(newht._tables, NULL, sizeof(HashNode*) * newht._N);
for (size_t i = 0; i < newht._N; i++)
{
HashNode* cur = newht._tables[i];
while (cur)
{
size_t Nnewindex = HashFunc(cur->_key, newht._N);
cur->_next = newht._tables[i];
cur = cur->_next;
}
}
free(ht->_tables);
ht->_N = newht._N;
ht->_tables = newht._tables;
}
}
int HashTableInsert(HashTable* ht, KeyType key, ValueType value)
{
size_t index = HashFunc(key, ht->_N);
if (ht->_tables[index] == NULL)
{
ht->_tables[index] = BuyHashNode(key, value);
ht->_size++;
return 0;
}
Check_Capacity(ht);
HashNode* cur = ht->_tables[index];
while (cur)
{
if (cur->_key == key)
{
return -1;
}
cur = cur->_next;
}
//头插
HashNode *node = BuyHashNode(key, value);
node->_next = ht->_tables[index];
ht->_tables[index] = node;
++ht->_size;
return 0;
}
HashNode* HashTableFind(HashTable* ht, KeyType key)
{
size_t index = HashFunc(key, ht->_N);
HashNode* cur = ht->_tables[index];
while (cur)
{
if (cur->_key == key)
{
return cur;
break;
}
cur = cur->_next;
}
return NULL;
}
int HashTableRemove(HashTable* ht, KeyType key)
{
size_t index = HashFunc(key, ht->_N);
HashNode* cur = ht->_tables[index];
if (cur->_key == key)
{
cur = cur->_next;
free(cur);
return 1;
}
else
{
HashNode* tmp = cur;
cur = cur->_next;
while (cur)
{
if (cur->_key == key)
{
tmp->_next = cur->_next;
free(cur);
return 1;
}
tmp = cur;
cur = cur->_next;
}
return -1;
}
}
void HashTableDestory(HashTable* ht)
{
for (size_t i = 0; i < ht->_N; i++)
{
HashNode* cur = ht->_tables[i];
while (cur)
{
HashNode* tmp = cur;
cur = cur->_next;
free(tmp);
}
}
free(ht->_tables);
ht->_N = 0;
ht->_size = 0;
}
void Printhas(HashTable* ht)
{
for (size_t i = 0; i < ht->_N; i++)
{
HashNode* cur = ht->_tables[i];
while (cur)
{
printf("%d->",cur->_key);
cur = cur->_next;
}
printf("\n");
}
}
数据结构—哈希表拉链法(数据类型int)
最新推荐文章于 2023-05-17 15:34:59 发布