简单哈希的C实现

纯练手,以下代码使用链地址法进行冲突处理

1. 头文件

#ifndef HASH_H_INCLUDED
#define HASH_H_INCLUDED

#define MAX_SIZE        100

typedef int Element;

typedef struct hash_node
{
    Element data;
    struct hash_node *next;
}hash_node, *hnode, *hlist;

typedef struct hash_tab
{
    Element tsize;
    hash_node **tlist;
}hash_tab, *htable;

hnode init_node();
htable init_table(int size);
int hash_int(Element val, int size);
hnode search_hnode_int(htable ht, Element val);

void add_hnode(htable ht, Element val);
void display_htable_simple(htable ht);
void display_htable_all(htable ht);
#endif // HASH_H_INCLUDED

2.实现文件

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "hash.h"

hnode init_node()
{
    hnode hn;
    if((hn = (hnode)malloc(sizeof(hash_node))) == NULL)
    {
        fprintf(stderr, "Init hash list error %s: %d\n", __FILE__, __LINE__);
        return NULL;
    }
    return hn;
}

htable init_table(int size)
{
    int i;
    htable ht;
    if((ht = (htable)malloc(sizeof(hash_tab))) == NULL)
    {
        fprintf(stderr, "Init hash table error %s: %d\n", __FILE__, __LINE__);
        return NULL;
    }
    ht->tsize = size;
    ht->tlist = (hlist *)malloc(sizeof(hlist) * size);
    for(i=0; i<size; i++)
    {
        ht->tlist[i] = init_node();
        ht->tlist[i]->data = i;
        ht->tlist[i]->next = NULL;
    }
    return ht;
}

int hash_int(Element val, int size)
{
    int key = val % size;
    return key;
}

// Brian Kernighan & Dennis Ritchie' hash algorithm used in Java
long hash_string(Element *val, int size)
{
    long hash, ch;
    while((ch = (long)*val++) != NULL)
    {
        hash = hash * 31 + ch;
    }
    return hash;
}

hnode search_hnode_int(htable ht, Element val)
{
    int key = hash_int(val, ht->tsize);
    hnode hn = ht->tlist[key];
    hn = hn->next;
    while(hn != NULL && hn->data != val)
        hn = hn->next;
    return hn;
}

hnode search_hnode_string(htable ht, Element *val)
{
    int key = hash_string(val, ht->tsize);
    hnode hn = ht->tlist[key];
    hn = hn->next;
    while(hn != NULL && strcpy(hn->data, val))
        hn = hn->next;
    return hn;
}

// add hash node at the beginning of the hash list
void add_hnode(htable ht, Element val)
{
    int key = hash_int(val, ht->tsize);
    hnode hn = init_node();
    // hnode tail;
    if(search_hnode_int(ht, val) == NULL)
    {
        hn->data = val;
        hn->next = ht->tlist[key]->next;
        ht->tlist[key]->next = hn;
    }
}

void display_htable_simple(htable ht)
{
    int i;
    hnode hn;
    for(i=0; i<ht->tsize; i++)
    {
        if(ht->tlist[i]->next == NULL)
            continue;
        // printf("node %2d: ", ht->tlist[i]->data);
        printf("node %2d: ", i);
        hn = ht->tlist[i]->next;
        while(hn)
        {
            printf("->%d ", hn->data);
            hn = hn->next;
        }
        printf("\n");
    }
}

void display_htable_all(htable ht)
{
    int i;
    hnode hn;
    for(i=0; i<ht->tsize; i++)
    {
        printf("node %d: ", ht->tlist[i]->data);
        hn = ht->tlist[i]->next;
        while(hn)
        {
            printf("->%d ", hn->data);
            hn = hn->next;
        }
        printf("\n");
    }
}

/*
// get intersection of the given two arrays
void insersection(int *a, int *b, int lena, int lenb)
{
    int i, j;
    htable ht = init_table(lena+lenb);
    for(i=0, j=0; i<lena, j<lenb; i++, j++)
    {
        add_hnode(ht, lena[i]);
        add_hnode(ht, lena[j]);
    }
}
*/

3. 测试文件

#include "hash.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE        100

int main()
{
    htable ht = init_table(MAX_SIZE);
    // display_htable(ht);
    add_hnode(ht, 1);
    add_hnode(ht, 24);
    add_hnode(ht, 25);
    add_hnode(ht, 9);
    add_hnode(ht, 172);
    add_hnode(ht, 600);
    add_hnode(ht, 700);
    add_hnode(ht, 800);
    add_hnode(ht, 1025);
    add_hnode(ht, 800);
    display_htable_simple(ht);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值