分离链接散列表--C语言实现

hashtable.h

#ifndef _HASHTABLE_H_
#define _HASHTABLE_H_
#define MinTableSize 100
#define Prime 10007

struct ListNode;
typedef struct ListNode* Position;
struct HashTbl;
typedef struct HashTbl* HashTable;
typedef int ElementType;
typedef Position List;

HashTable InitializeTable(int TableSize);
void DestroyTable(HashTable H);
Position Find(ElementType Key, HashTable H);
void Insert(ElementType Key, HashTable H);
ElementType Retrieve(Position P);

#endif

hashtable.c

#include<stdio.h>
#include<stdlib.h>
#include"hashtable.h"

struct ListNode
{
    ElementType Element;
    Position Next;
};
struct HashTbl
{
    int TableSize;
    List* TheLists;
};

int Hash(ElementType Key,int TableSize)
{
    return Key%TableSize;//简化问题,把Key当做数字处理
}
HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;
    if (TableSize < MinTableSize)
    {
        printf("ERROR!\n");
        return NULL;
    }
    H = (HashTable)malloc(sizeof(struct HashTbl));
    if (H == NULL)
    {
        printf("FAILURE!\n");
        exit(EXIT_FAILURE);
    }
    H->TableSize = Prime;//Prime 最好是一个根据TableSize变化的质数。
    H->TheLists = (List *)malloc(sizeof(List)*H->TableSize);
    if (H->TheLists == NULL)
    {
        printf("FAILURE!\n");
        exit(EXIT_FAILURE);
    }
    for (i = 0; i < H->TableSize;i++)
    { 
        H->TheLists[i] = (List)malloc(sizeof(struct ListNode));
        if (H->TheLists[i] == NULL)
        {
            printf("FAILURE!\n");
            exit(EXIT_FAILURE);
        }
        else
            H->TheLists[i]->Next = NULL;
    }
    return H;
}
void DestroyTable(HashTable H)
{
    for (int i = 0; i < H->TableSize; i++)
    {
        Position temp=H->TheLists[i]->Next;
        H->TheLists[i]->Next = NULL;
        while (temp != NULL)
        {
            Position t = temp->Next;
            free(temp);
            temp = t;
        }
    }
}
Position Find(ElementType Key, HashTable H)
{
    Position P;
    List L;
    L = H->TheLists[Hash(Key, H->TableSize)];
    P = L->Next;
    while (P != NULL && P->Element != Key)
        P = P->Next;
    return P;
}
void Insert(ElementType Key, HashTable H)
{
    Position Pos, NewCell;
    List L;
    Pos = Find(Key, H);
    if (Pos == NULL)
    {
        NewCell = (Position)malloc(sizeof(struct ListNode));
        if (NewCell == NULL)
        {
            printf("FAILURE!\n");
            exit(EXIT_FAILURE);
        }
        else
        {
            L = H->TheLists[Hash(Key, H->TableSize)];
            NewCell->Element = Key;
            NewCell->Next = L->Next;
            L->Next = NewCell;
        }
    }
}
ElementType Retrieve(Position P)
{
    if(!P)
        return P->Element;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值