哈希表简单实现

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

typedef struct _node{
    char *name;
    char *desc;
    struct _node *next;
}node;

#define HASHSIZE 10
static node* hashtab[HASHSIZE];

void inithashtab(){
    int i;
    for(i=0;i<HASHSIZE;i++)
        hashtab[i]=NULL;
}

//计算hash值
unsigned int hash(char *s){
    unsigned int h=0;
    for(;*s;s++)
        h=*s+h*31;
    return h%HASHSIZE;
}

//如有重名返回其在hash表中位置
//没有重名返回null
node* lookup(char *n){
    unsigned int hi=hash(n);
    node* np=hashtab[hi];
    for(;np!=NULL;np=np->next){
        if(!strcmp(np->name,n))
            return np;
    }
    
    return NULL;
}

char* m_strdup(char *o){
    int l=strlen(o)+1;
    char *ns=(char*)malloc(l*sizeof(char));
    strcpy(ns,o);
    if(ns==NULL)
        return NULL;
    else
        return ns;
}

char* get(char* name){
    node* n=lookup(name);
    if(n==NULL)
        return NULL;
    else
        return n->desc;
}

int install(char* name,char* desc){
    unsigned int hi;
    node* np;
    if((np=lookup(name))==NULL){
        hi=hash(name);
        np=(node*)malloc(sizeof(node));
        if(np==NULL)
            return 0;
        np->name=m_strdup(name);
        if(np->name==NULL) return 0;
        np->next=hashtab[hi];//--将原来的节点的地址储存到next指针
        hashtab[hi]=np;//--将新节点的地址储存到哈希表头
        //以上两行代码实现新节点在表头的插入
    }
    else
        free(np->desc);
    np->desc=m_strdup(desc);
    if(np->desc==NULL) return 0;
    
    return 1;
}

int remov(char* name){
        unsigned int hi;
        node* np;
        node* npl;

        hi=hash(name);
        if (NULL == hashtab[hi]) {
            printf("%s not in list\n", name);
            return 0;
        }
        np=(hashtab[hi]);
        
        while((0 != strcmp(np->name,name)) && (NULL != np->next)){
            npl = np;
            np = np->next;
        }
                
        if(!strcmp(np->name,name)){
            if(np == hashtab[hi]){
                 hashtab[hi] = np->next;
            }
            else {
                npl->next = np->next;
            }
            
            free(np->name);
            free(np->desc);
            free(np);
        
            return 1;
        }

        return 0;
}

/* A pretty useless but good debugging function,
which simply displays the hashtable in (key.value) pairs
*/
void displaytable(){
    int i;
    node *t;
    printf("----------------------\n");
    for(i=0;i<HASHSIZE;i++){
        if(hashtab[i]==NULL)
            printf("()");
        else{
            t=hashtab[i];
            printf("(");
            for(;t!=NULL;t=t->next)
                printf("(%s.%s) ",t->name,t->desc);
            printf(".)");
        }
        printf("\n");
    }
    printf("=======================\n");
}

void cleanup(){
    int i;
    node *np,*t;
    for(i=0;i<HASHSIZE;i++){
        if(hashtab[i]!=NULL){
            np=hashtab[i];
            while(np!=NULL){
                t=np->next;
                free(np->name);
                free(np->desc);
                free(np);
                np=t;
            }
        }
    }
}

int main(){
    int i;
    char* names[]={"name","address","phone","k1110","k1101","k1011"};
    char* descs[]={"Sourav","Sinagor","26300788","Value1","Value2","Value3"};
    
    inithashtab();
    for(i=0;i<6;i++)
        install(names[i],descs[i]);
    
    printf("Done\n");
    displaytable();
    printf("If we didnt do anything wrong..""we should see %s\n",get("k1110"));
    
    install("phone","9433120451");
    install("k0111","Value4");
    displaytable();
    printf("Again if we go right, we have %s and %s\n",get("k1110"),get("phone"));

    printf("remove k1101 %d,remove ph %d\n", remov("k1101"), remov("ph"));
    displaytable();

    /*displaytable();*/
    cleanup();
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值