数据结构与算法分析-分离链接散列表的实现

简介: #include #include typedef char* ElementType; typedef unsigned int Index; #define MinTableSize 15 struct ListNode; typedef struct ListNode ...
#include<stdio.h>
#include<math.h>

typedef char* ElementType;
typedef unsigned int Index;
#define MinTableSize 15

struct ListNode;
typedef struct ListNode *Position;
struct HashTbl;
typedef struct HashTbl *HashTable;

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

struct ListNode
{
    ElementType Element;
    Position next;
};

typedef Position List;

struct HashTbl
{
    int TableSize;
    List *TheLists;
};

Index Hash(const char *key,int TableSize)
{
    unsigned int HashVal=0;
    while(*key!='\0')
    {
        HashVal+=*key++;
    }
    return HashVal%TableSize;
}

int NextPrime(int TableSize)
{
    int i,j=2;
    for(i=TableSize;i>0;i--)
    {
        while(j<sqrt(i))
        {
            if(i%j==0)
                break;
            j++;
        }
        if(j==sqrt(i))
            break;
    }
    return i;
}

HashTable InitializeTable(int TableSize)
{
    HashTable H;
    int i;
    if(TableSize<MinTableSize)
    {
        Error("Table size too small");
        return NULL;
    }
    H=malloc(sizeof(struct HashTbl));
    if(H==NULL)
    {
        FatalError("Out of space!!");
    }
    H->TableSize=NextPrime(TableSize);
    H->TheLists=malloc(sizeof(List)*H->TableSize);
    if(H->TheLists==NULL)
    {
        FatalError("Out of space!!");
    }
    for(i=0;i<H->TableSize;i++)
    {
        //分配头节点
        H->TheLists[i]=malloc(sizeof(struct ListNode));
        if(H->TheLists[i]==NULL)
            FatalError("Out of space!!");
        else
            H->TheLists[i]->next=NULL;
    }
    return H;
}

void DestroyTable(HashTable H)
{
    int i;
    for(i=0;i<H->TableSize;i++)
    {
        free(H->TheLists[i]);
    }
    free(H->TheLists);
    free(H);
}

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=malloc(sizeof(struct ListNode));
        if(NewCell==NULL)
        {
            FatalError("Out of space!!");
        }
        else
        {
            L=H->TheLists[Hash(key,H->TableSize)];
            NewCell->next=L->next;
            NewCell->Element=key;
            L->next=NewCell;
        }
    }
}

ElementType Retrieve(Position P)
{
    if(P==NULL)
        return NULL;
    else
        return P->Element;
}

void Delete(ElementType key,HashTable H)
{
    Position Pos,Pre;
    List L;
    int i;
    Pos=Find(key,H);
    if(Pos==NULL)
        return;
    else
    {
        L=H->TheLists[Hash(key,H->TableSize)];
        for(Pre=L;pre->next!=NUll;Pre=Pre->next)
        {
            if(Pre->next==Pos)
            {
                free(Pos->Element);
                free(Pos);
                break;
            }
        }
    }
}

 

相关文章
|
2月前
|
存储 算法 Java
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
52 1
[Java]散列表的数据结构以及对象在JVM堆中的存储过程
|
4月前
|
存储 缓存 数据库
Python高级数据结构——散列表(Hash Table)
Python高级数据结构——散列表(Hash Table)
76 1
Python高级数据结构——散列表(Hash Table)
|
7月前
|
存储 算法 决策智能
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(下)
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(下)
48 0
|
7月前
|
算法 C++ 容器
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(上)
(万字,细细阅读)竞赛算法入门必经算法模型(附带题目链接和模板)(上)
27 0
|
9月前
|
算法
【MATLAB第22期】基于MATLAB的xgboost算法多输入多输出回归模型 已购用户可在之前下载链接免费获取
【MATLAB第22期】基于MATLAB的xgboost算法多输入多输出回归模型 已购用户可在之前下载链接免费获取
|
2月前
|
存储 缓存 索引
【数据结构入门精讲 | 第十四篇】散列表知识点及考研408、企业面试练习(1)
【数据结构入门精讲 | 第十四篇】散列表知识点及考研408、企业面试练习(1)
25 0
|
2月前
|
自然语言处理 数据安全/隐私保护
【数据结构入门精讲 | 第十五篇】散列表知识点及考研408、企业面试练习(2)
【数据结构入门精讲 | 第十五篇】散列表知识点及考研408、企业面试练习(2)
23 0
|
4月前
|
存储 算法 索引
Python 数据结构和算法:什么是散列表(Hash Table)?在 Python 中如何实现?
Python 数据结构和算法:什么是散列表(Hash Table)?在 Python 中如何实现?
|
9月前
|
算法 定位技术
连连看核心算法与基本思想(附全部项目代码链接与代码详细注释)
连连看核心算法与基本思想(附全部项目代码链接与代码详细注释)
198 0
|
4月前
|
人工智能 算法 C语言
【408数据结构与算法】—线性表的定义和分析(二)
【408数据结构与算法】—线性表的定义和分析(二)