Hash算法实现

来源:互联网 发布:大学生旅游消费大数据 编辑:程序博客网 时间:2024/06/16 06:24

一、代码说明

1、当冲突次数达到一定程度,需要重建哈希表。这个没做。

2、哈希表长直接用宏表示,为了程序更好的兼容性,需要用一个增量数组表示。

3、代码只实现最简单的功能,目的是理解hash表的建立、查找、插入、哈希函数、冲突处理。

 

二、C代码实现

#include <stdio.h>#include <malloc.h>typedef int elemtype;typedef int status;#define INIT_HASH_SIZE  11 //997#define INIT_HASH_VALUE -32768#define SEARCH_OK      1 /* search success */#define SEARCH_FAIL    0 /* search fail */#define INSERT_OK      1 /* insert success */#define INSERT_EXIST   0 /* insert fail for it exists */#define INSERT_FAIL   -1 /* insert fail for overwhelming  collision times */#define OK    1#define ERROR 0struct hashtable{    elemtype *elem;    int hashsize; /* hash table size */    int count;    /* current size    */};status build_hashtable(struct hashtable *HT){    int i;    (*HT).elem = (elemtype*)malloc(sizeof(elemtype) * INIT_HASH_SIZE);    if (!(*HT).elem)    {        printf("%s: malloc failed\n", __FUNCTION__);        return ERROR;     }    (*HT).hashsize = INIT_HASH_SIZE;    for (i = 0; i < (*HT).hashsize; i++)    {        (*HT).elem[i] = INIT_HASH_VALUE;    }    (*HT).count = 0;    return OK;}int hash_func(elemtype e){    return e % INIT_HASH_SIZE; }int hash_collison(elemtype e/* int pos */, int collision_count){    return (hash_func(e)/* pos */ + collision_count) % INIT_HASH_SIZE;}status search_hashtable(struct hashtable HT, elemtype e, int *pos, int *collision_count){    *collision_count = 0;    *pos = hash_func(e);    while (HT.elem[*pos] != INIT_HASH_VALUE && HT.elem[*pos] != e)    {        *pos = hash_collison(e/* *pos */, ++(*collision_count));         }    if (HT.elem[*pos] == e)    {        return SEARCH_OK;    }    else    {        return SEARCH_FAIL;    }}status insert_hashtable(struct hashtable *HT, elemtype e){    int pos;    int collision_count;       int ret;    ret = search_hashtable(*HT, e, &pos, &collision_count);    printf("[debug] pos is %d, collision_count is %d\n", pos, collision_count);    if (ret)        {        return INSERT_EXIST;    }    else if (collision_count < INIT_HASH_SIZE / 2)    {        (*HT).elem[pos] = e;        (*HT).count++;        return INSERT_OK;    }    else    {        printf("overwhelming collison times, and the hash table should be recreated!\n\n");        return INSERT_FAIL;    }}void print_hashtable(struct hashtable HT){    int i;    printf("hash table current size: %3d\n", HT.count);    printf("hash table totol   size: %3d\n", HT.hashsize);    printf("hash table elem:\n");    for (i = 0; i < 11; i++)    {        if (HT.elem[i] != INIT_HASH_VALUE)        {            printf("pos = %d, the value is %d\n", i, HT.elem[i]);        }    }}int main(void){    struct hashtable HT;    int key;    build_hashtable(&HT);    printf("please input the key what you want to insert!\n");    scanf("%d", &key);    while (key != INIT_HASH_VALUE)    {        /* 插入失败,因为冲突太多,需要重建hashtable,此处退出输入的循环 */        if (INSERT_FAIL == insert_hashtable(&HT, key))        {            break;        }        scanf("%d", &key);    }    print_hashtable(HT);    return 0;}

 

3、调试界图