如下图所示,为哈希表的开散列存储方式的基本结构:
通过哈希函数对key值,求出对应的哈希值作为数组的下标,相同的哈希值存储在同一个桶中,桶中元素按照从小到大排列:
哈希函数使用了除留余数法,选择与桶的大小最相近的一个质数。
具体代码实现如下:
hashlist.c
#include <stdio.h>
#include <stdlib.h>
#include "hashlist.h"
static int hash_fun(int key)
{
return key % 11;
}
hashMap_t *hash_create(const int size)
{
if (size <= 0 ) {
return NULL;
}
hashMap_t *h = (hashMap_t *)malloc(sizeof(hashMap_t));
if(h == NULL) {
perror("hashMap malloc error:");
exit(1);
}
h->hashtab = (listhashNode_t **)malloc(sizeof(listhashNode_t *) * size);
if(h->hashtab == NULL) {
perror("listhashNode_t ** malloc error :");
exit(1);
}
for(int i = 0; i < size; i++) {
h->hashtab[i] = NULL;
}
h->size = size;
h->num = 0;
return h;
}
void hash_destory(hashMap_t *h)
{
if(h == NULL) {
return ;
}
listhashNode_t *cur = NULL;
listhashNode_t *tmp = NULL;
for(int i = 0; i < h->size; i++) {
cur = h->hashtab[i];
while(cur != NULL) {
tmp = cur;
cur = cur->next;
free(tmp);
}
h->hashtab[i] = NULL;
}
free(h->hashtab);
free(h);
}
int hash_insert(hashMap_t *h, int key, int data)
{
if(h == NULL) {
return -1;
}
int hashindex = hash_fun(key);
listhashNode_t *cur = h->hashtab[hashindex];
listhashNode_t *prev = NULL;
listhashNode_t *pNode = (listhashNode_t *)malloc(sizeof(listhashNode_t));
if(pNode == NULL) {
perror("pNode malloc error:");
exit(1);
}
pNode->data = data;
pNode->key = key;
pNode->next = NULL;
while(cur != NULL) {
if(cur->key < key) {
prev = cur;
cur = cur->next;
} else if(cur->key == key) {
return -2;
} else {
break;
}
}
if(prev == NULL) { //该哈希值处没有节点,或者插在第一个
pNode->next = h->hashtab[hashindex];
h->hashtab[hashindex] = pNode;
} else {
pNode->next = prev->next;
prev->next = pNode;
}
h->num ++;
return 0;
}
int hash_search(hashMap_t *h, int key)
{
if(h == NULL) {
return -1;
}
int hashindex = hash_fun(key);
listhashNode_t *cur = h->hashtab[hashindex];
while(cur != NULL) {
if(cur->key == key) {
return cur->data;
}
cur = cur->next;
}
return 0;//没找到
}
listhashNode_t *hash_delete(hashMap_t *h, int key)
{
if(h == NULL) {
return NULL;
}
listhashNode_t *tmp = NULL;
int hashindex = hash_fun(key);
listhashNode_t *cur = h->hashtab[hashindex];
listhashNode_t *prev = NULL;
while(cur != NULL) {
if(cur->key == key) {
tmp = cur;
if(prev == NULL) {
h->hashtab[hashindex] = cur->next;
} else {
prev->next = cur->next;
}
return tmp;
}
prev = cur;
cur = cur->next;
}
h->num--;
return NULL;
}
void hash_dump(hashMap_t *h)
{
if(h == NULL) {
return ;
}
listhashNode_t *cur = NULL;
printf("hash table ,size :%d, num: %d\n", h->size, h->num);
for (int i = 0; i < h->size; i++) {
printf("table[%d] :", i);
cur = h->hashtab[i];
while(cur != NULL) {
printf("[%d]=[%d] ", cur->key, cur->data);
cur = cur->next;
}
printf("\n");
}
return ;
}
int main()
{
hashMap_t *h = hash_create(12);
if(h == NULL) {
printf("hash create failed\n");
return -1;
}
int key = 0;
int data = 0;
int ret = 0;
while(1) {
printf("please intput key val, -1 -1 to quit: ");
scanf("%d %d", &key, &data);
if((key == -1) && (data == -1)) {
break;
}
ret = hash_insert(h, key,data);
if(ret == -2) {
printf("insert error key is same, please input angin\n");
continue;
} else if(ret == -1) {
printf("something is not right, please check!");
} else {
printf("insert success [%d]=[%d]\n", key,data);
}
}
hash_dump(h);
while(1) {
printf("please input key to find the val ,-1 to quit:");
scanf("%d", &key);
if(key == -1) {
break;
}
data = hash_search(h, key);
if(data == 0) {
printf("key is not found!\n");
continue;
}
printf("val :%d\n", data);
}
listhashNode_t *ptmp = NULL;
while(1) {
printf("please intput the key to delete, -1 to quit :");
scanf("%d", &key);
if(key == -1) {
break;
}
ptmp = hash_delete(h, key);
if(ptmp == NULL) {
printf("not found the key!\n");
continue;
}
printf("delete %d success\n", key);
free(ptmp);
}
hash_dump(h);
hash_destory(h);
return 0;
}
hashlist.h
#ifndef LISTHASH_H_
#define LISTHASH_H_
typedef struct _listhahsNode {
int key;
int data;
struct _listhahsNode *next;
}listhashNode_t;
typedef struct _hashmap {
int size;
int num;
listhashNode_t **hashtab;
}hashMap_t;
hashMap_t *hash_create(const int size);
void hash_destory(hashMap_t *h);
int hash_insert(hashMap_t *h, int key, int data);
int hash_search(hashMap_t *h, int key);
listhashNode_t *hash_delete(hashMap_t *h, int key);
void hash_dump(hashMap_t *h);
#endif
输入如下: