通用链表实现哈希表
hash** :table_one-----table_two-------table_three-------table_four
hashnode1 hashnode2 hashnode3 hashnode4
hashnode1 hashnode2 hashnode3 hashnode4
hashnode1 hashnode2 hashnode3 hashnode4
hashnode1 hashnode2 hashnode3 hashnode4
实现形式:链表头组+链表 组合
hashtable.c 是hashtable.h的实现
hashTest.c调用hashtable.h里面提供的接口,完成哈希链表的构建和增删改查等
好处:
- 不会因为冲突导致结点移位
- 复杂度O(n/m)
hashtable.h
#include "dlist.h"
typedef enum HashRet
{
HAMS_OK, //成功
HAMS_OOM, //内存溢出(OutOfMemory)
HAMS_STOP, //停止
HAMS_ERROR, //
HAMS_FAIL //失败
}HashRet;
typedef int (*HashKeyPositionFunc)(void* key);
typedef struct DList Hash;
typedef struct DListNode HashNode;
typedef struct HashOpertion{
Hash** head;
int count;
Hash** (*Init)(int);
HashNode *(*search)(Hash *,DListDataCompareFunc ,void *);
int (*HashAdd)(Hash **,HashKeyPositionFunc,DListDataCompareFunc, void* key,void * value);
void (*PrintHash)(Hash **,DListDataVisitFunc ,int);
void (*destroyHash)(Hash **,int );
}HashOpertion;
extern void get_HashOperation(HashOpertion* operation);
hashtable.c
#include "./include/hashTable.h"
unsigned int GetHashPosition(int num)
{
if (num < 0) {
return ~(--num);
}
return num;
}
Hash** InitHash(int length)
{
/* byteLength 用于存储传入 length 的字节数, 申请哈希表堆内存使用 */
unsigned int byteLength;
Hash *hh = NULL;
if (length == 0) {
printf("parameter error!\n");
return NULL