数据结构—哈希表拉链法(数据类型int)

#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");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值