简单哈希的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 
#include 
#include 
#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; itlist[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; itsize; 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; itsize; 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

3. 测试文件

#include "hash.h"
#include 
#include 
#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;
}


你可能感兴趣的:(简单哈希的C实现)