首页 > 代码库 > hash表的创建

hash表的创建

功能:创建一个hash table,如果有处理冲突,则采用再散列法放置该元素

代码参考《零基础学数据结构》

代码如下:

root@ubuntu:/mnt/shared/appbox/hash# cat hash.c
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <malloc.h>

typedef int KeyType;

typedef struct 
{
        KeyType key;    /* key value */
        int hi; /*  hash counts */
}DataType;

typedef struct 
{
        DataType *data;
        int tableSize;  /* hash table len */
        int curSize;    /* key value numbers */
}HashTable;

void DisplayHash(HashTable *H, int m);

/*
* H:hash table pointer
* m: hashtable len
* p: devided numbers
* hash: be hashed data (src data)
* n: number of key values
*/
void CreateHash(HashTable *H, int m, int p, int hash[], int n)
{
        int i, sum, addr, di, k = 1;/* k: ?/

        H->data = http://www.mamicode.com/(DataType *)malloc(m * sizeof(DataType));>
输出结果:

root@ubuntu:/mnt/shared/appbox/hash# ./hash    
[line:59] addr:1, i=0, key=23
[line:59] addr:2, i=1, key=35
[line:70] di:3, i=2, key=12
[line:70] di:4, i=3, key=56
[line:70] di:5, i=4, key=123
[line:59] addr:6, i=5, key=39
[line:70] di:7, i=6, key=342
[line:70] di:8, i=7, key=90
hash index:     0    1    2    3    4    5    6    7    8    9    10   
key value:      -1   23   35   12   56   123  39   342  90   -1   -1   
hash times:     0    1    1    3    4    4    1    7    7    0    0   


hash表的创建