通用链表实现哈希表

本文详细介绍了如何使用通用链表实现哈希表的数据结构,包括初始化、查找、添加和打印操作。通过链表头组和链表组合的方式,避免了冲突导致的结点移位,查找和插入操作的时间复杂度为O(n/m)。通过hashTest.c示例展示了如何调用这些接口进行实际操作。
摘要由CSDN通过智能技术生成

通用链表实现哈希表

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值