c语言实现hashtable,类似C++的map和iOS的NSDictionary

本文介绍如何使用C语言实现一个哈希表(HashTable),作为快速查找的数据结构,其内部采用链表处理数组。内容包括如何指定表的长度、遍历哈希表的方法,以及在处理多个键值时采用的动态数组dyArray。文章还提到了哈希表中选择合适表长度的策略,并提供了相关的代码参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

跟线性数组和链表不同,HashTable是快速查找的数据结构。本文中的HashTable使用链表处理数组。

该HashTable可以指定table的长度,提供了遍历的方法。包括table的长度的选择也比较讲究。

其他c的数据结构实现,dyArray参考点击打开链接  treeStruct参考点击打开链接

	cp_int32 nPrime[MAX_HASH_PRIME_ARRAY_NUM] = {
		17,   
		37,   
		79,   
		163,  
		331,  
		673,  
		1361  
	};
就是说table的长度来取自上面这个数组。比如用户设定了200,那么table的长度就是331,找到第一次比输入值大的数值。可以注意到上面的都是素数。

下面是具体的代码贴出来,共大家参考。

代码中有个接口是查到多个key使用到了动态数组dyArray,参考上一篇点击打开链接

//
//  cpPlatform.h
//  dataStruct
//
//  Created by hherima on 14-7-29.
//  Copyright (c) 2014年 . All rights reserved.
//

#ifndef dataStruct_cpPlatform_h
#define dataStruct_cpPlatform_h

enum
{
    CP_FALSE  =   0,
    CP_TRUE    =  !CP_FALSE
};


#define  F_MALLOC_TYPE(s) (s*)f_malloc(sizeof(s))
#define  FREEFUN free
#define  MIN_PRE_ALLOCATE_SIZE 10  //The initial size of the dynamic array.
#define  MEMSETFUN      memset
#define  REALLOCFUN     realloc
#define  MALLOCFUN      malloc
#define  MEMCMPFUN      memcmp
#define  MEMCPYFUN      memcpy


typedef unsigned char cp_bool;
typedef signed int cp_int32;
typedef char cp_int8;
typedef unsigned int cp_uint32;


#endif
上面是数据类型的定义,为了跨平台使用

下面是hashtable的数据结构头文件

//
//  hashStruct.h
//  dataStruct
//
//  Created by hherima on 14-7-29.
//  Copyright (c) 2014年 . All rights reserved.
//

#ifndef dataStruct_hashStruct_h
#define dataStruct_hashStruct_h
#include <stdlib.h>
#include "cpPlatform.h"
#include "dyArray.h"

struct Node
{
    cp_int8* m_pKey; //the hash key.
    cp_int32 m_nKeyLength; // the length of the hash key in bytes.
    void* m_pVal;//hash_node value
    struct Node* m_pNext;
};//hash table,use link list to process conflict

struct HashTable
{
    cp_int32 m_nLength; //hash table's length.
    cp_int32 m_nEleNum; //hash table's element count.
    struct Node **m_ppHead;//the hash table's array to record all key's value.
};

cp_int32 GetHashFileNum(cp_int8* pKey, cp_int32 nKeyLength);

struct Node* HashFindOne(cp_int8* pKey,cp_int32 nKeyLength,struct HashTable *pTable);//search

cp_int32 HashFindMultkeyredund(cp_int8* pKey,cp_int32 nKeyLength,struct HashTable *pTable,struct DynamicArray *pdestArray);
void HashInsertOne(cp_int8* pKey,cp_int32 nKeyLength,void* pVal,struct HashTable *pTable);//insert
void HashInsertOneKeyredund(char* pKey,cp_int32 nKeyLength,void* pVal,struct HashTable *pTable);

void HashDelOne(struct HashTable * pTable,void (*pFreeFunc) (void *),cp_int8 *pKey,cp_int32 nKeyLength);

struct HashTable * HashTableCreate(cp_int32 nKeyNum);
struct Node* HashGetFirst(struct HashTable *pTable);
struct Node* HashGetNext(struct HashTable *pTable,cp_int8* pKey,cp_int32 nKeyLength);

cp_int32 HashGetLength(struct HashTable *pTable);
void HashTableVisit(struct HashTable * pTable,void (*pVisitFunc) (void *,void *),void *pAgent);
void HashTableReset(struct HashTable * pTable,void (*pFreeFunc) (void *));

void HashTableDestroy(struct HashTable * pTable,void (*pFreeFunc) (void *));

cp_int32 HashGetValue(cp_int8* pKey,cp_int32 nKeyLength,cp_int32 nTableLength);

#endif

源文件

//
//  hashStruct.c
//  dataStruct
//
//  Created by hherima on 14-7-29.
//  Copyright (c) 2014年 . All rights reserved.
//
#include "hashStruct.h"

#define MAX_HASH_PRIME_ARRAY_NUM    7
/**************************************************************************************************
 function name:
 HashGetValue
 description:
 the hash arithmetic to get the position of the certain key.
 parameter:
 pKey: the exclusive symbol which is related to a data block in hash table.
 nKeyLength: the key string's length.
 nTableLength: the hash table's length.
 return value:
 the position of the certain key.
 ***************************************************************************************************/

cp_int32 HashGetValue(cp_int8* pKey,cp_int32 nKeyLength,cp_int32 nTableLength)
{
	cp_uint32 h = 0;
	cp_uint32 g = 0;
    
	if(!pKey || nKeyLength<1 || nTableLength<1) //if the input parameter is invalid, return.
    {
        return -1;
    }
	while(nKeyLength--) // get each charactor and use it to compute hash value.
	{
		h = (h<<4) + *pKey++;
		g = h & 0xF0000000L;
		if(g)
		{
			h ^= g>>24;
		}
		h &= ~g;
	}
	return h % nTableLength;
}

/**************************************************************************************************
 function name:
 HashFindOne
 description:
 search the position's node of the certa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值