数据结构学习——散列表ADT(程序化)

不知道大家在刚开始看《数据结构与算法分析——C语言描述》散列一章的时候,能不能理解书上的内容,小ZZ看的时候就没怎么看明白。于是就去问度娘,度娘给了我这样一篇文章。里面介绍了散列的基本定义以及常用的实现方法,比较通俗易懂(可能是智商有点拙计)。

http://blog.csdn.net/u010275850/article/details/44963701

先看完了这篇博文,然后再看书上的结论,顿时恍然大悟有木有啊。

参考了书上的程序思路,下面将散列表的常用操作程序化如下:

/*分离链接法构造散列*/
#include<stdio.h>
#include<stdlib.h>

#define TbSize 10

typedef struct ListNode
{
    int data;
    struct ListNode *next;
}List,*list;

typedef struct HashNode
{
    int TableSize;
    list *TheLists;
}Hash,*HT;

HT InitHT(int tablesize);//初始化例程
list find(int data,HT H);//查找操作
int hash(int data,int tablesize);//哈希函数
void insert(int data,HT H);//插入操作
void Delete(int data,HT H);//删除操作
void PrintHt(HT H);//打印整个哈希表
void PrintList(list L);//打印链表

int main()
{
    int Data[10]={0,1,4,9,16,25,36,49,64,81};
    int i=0;
    HT h;
    h=InitHT(10);
    if(h==NULL)
        printf("Out of space!\n");
    for(i=0;i<10;i++)
    {
        insert(Data[i],h);
    }
    PrintHt(h);

    insert(7,h);
    insert(59,h);
    printf("after insert 7 and 59:\n");
    PrintHt(h);
    Delete(36,h);
    printf("after delete 36:\n");
    PrintHt(h);

    find(7,h);
    find(49,h);
    find(36,h);
    return 0;
}

HT InitHT(int tablesize)
{
    HT H;
    int i;
    H=malloc(sizeof(struct HashNode));
    if(H==NULL)
        printf("Out of space!\n");

    H->TableSize=TbSize;
    H->TheLists=malloc(sizeof(list)*H->TableSize);
    if(H->TheLists==NULL)
        printf("Out of space!\n");
    for(i=0;i<H->TableSize;i++)
    {
        H->TheLists[i]=malloc(sizeof(List));
        if(H->TheLists[i]==NULL)
             printf("Out of space!\n");
        else H->TheLists[i]->next=NULL;
    }
    return H;
}

list find(int data,HT H)
{
    list tmp;
    tmp=H->TheLists[hash(data,H->TableSize)];//得到的只是一个表头指针
    tmp=tmp->next;
    while( (tmp!=NULL) && (data!=tmp->data) )
        tmp=tmp->next;
    if(tmp==NULL)
        printf("%d has not find!\n",data);
    else  printf("%d has find!\n",data);
    return tmp;
}

int hash(int data,int tablesize)
{
    return data%tablesize;
}

void insert(int data,HT H)
{
    list p,newcell,L;
    p=find(data,H);
    if(p==NULL)//说明原表中没有
    {
        newcell=malloc(sizeof(List));
        if(newcell==NULL)
            printf("Out of space!\n");
        else
        {
            L=H->TheLists[hash(data,H->TableSize)];
            newcell->data=data;
            newcell->next=L->next;
            L->next=newcell;//每次插入到最前端
        }
    }
}

void Delete(int data,HT H)
{
    list pre=NULL,cur;//前驱元 当前元

    cur=H->TheLists[hash(data,H->TableSize)];
    pre=cur;
    cur=cur->next;
    while((cur!=NULL)&&(cur->data!=data))
    {
       pre=cur;
       cur=cur->next;
    }
    if(cur==NULL)
        printf("%d hasn't find\n",data);
    else
    {
       if(pre!=NULL)
       {
           pre->next=cur->next;
           free(cur);
       }
    }
}

void PrintHt(HT H)
{
    int i=0;
    for(i=0;i<H->TableSize;i++)
    {
        PrintList(H->TheLists[i]);
        printf("\n");
    }
}

void PrintList(list L)
{
    if(L==NULL)
        printf("list is empty!");
    while(L)
    {
        printf("%d\t",L->data);
        L=L->next;
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值