代码:
BSTree.h
BSTree.c
二叉排序树(Binary Sort Tree) 又称为二叉查找树(Binary Search Tree)
Hash.h
#ifndef _HASH_H_
#define _HASH_H_
typedef void Hash;//定义哈希表类型
typedef void HashKey;//定义哈希表键类型
typedef void HashValue;//定义定义哈希表值类型
typedef int (Hash_Compare)(HashKey*, HashKey*);//定义参数为两个哈希表键类型指针返回值是整形的函数类型
Hash* Hash_Create();//声明创建哈希表函数
void Hash_Destroy(Hash* hash);//声明销毁哈希表函数
void Hash_Clear(Hash* hash);//声明清空哈希表函数
int Hash_Add(Hash* hash, HashKey* key, HashValue* value, Hash_Compare* compare);//声明添加值函数
HashValue* Hash_Remove (Hash* hash, HashKey* key, Hash_Compare* compare);//声明移除值函数
HashValue* Hash_Get(Hash* hash, HashKey* key, Hash_Compare* compare);//声明获取值函数
int Hash_Count(Hash* hash);//声明获取数量函数
#endif
Hash.c
#include <stdio.h>
#include <malloc.h>
#include "Hash.h"
#include "BSTree.h"
typedef struct _tag_HashNode HashNode;//定义哈希表节点类型
struct _tag_HashNode
{
BSTreeNode header;
HashValue* value;
};
//定义递归释放节点函数
void recursive_clear(BSTreeNode* node)
{
if( node != NULL )
{
recursive_clear(node->left);
recursive_clear(node->right);
free(node);
}
}
Hash* Hash_Create()//定义创建哈希表函数
{
return BSTree_Create();//创建一个树
}
void Hash_Destroy(Hash* hash)//定义销毁哈希表函数
{
Hash_Clear(hash);//清空哈希表
BSTree_Destroy(hash);//再销毁树
}
void Hash_Clear(Hash* hash)//定义清空哈希表函数
{
//先清除释放节点的空间。这释放的其实是HashNode类型的空间
//是在Hash_Add新建的node节点,因为转换类型成树的节点,但还是那块内存开头
//所以会释放HashNode中的Value.
recursive_clear(BSTree_Root(hash));
BSTree_Clear(hash);//再将树清空重置
}
//定义添加值函数
int Hash_Add(Hash* hash, HashKey* key, HashValue* value, Hash_Compare* compare)
{
int ret = 0;
HashNode* node = (HashNode*)malloc(sizeof(HashNode));//新建节点
if( ret = (node != NULL) )//创建成功
{
node->header.key = key;//将新建的键赋给树点的键
node->value = value;//将新建的值赋给哈希节点的值
ret = BSTree_Insert(hash, (BSTreeNode*)node, compare);//将哈希转换插入到树
if( !ret )//如果不成功释放新建节点
{
free(node);
}
}
return ret;
}
//定义移除值函数
HashValue* Hash_Remove(Hash* hash, HashKey* key, Hash_Compare* compare)
{
HashValue* ret = NULL;
HashNode* node = (HashNode*)BSTree_Delete(hash, key, compare);//从树中删除节点,并返回删除节点
if( node != NULL )//如果有该节点
{
ret = node->value;//取得节点内的值
free(node);//释放该节点
}
return ret;//返回值
}
//定义获取值函数
HashValue* Hash_Get(Hash* hash, HashKey* key, Hash_Compare* compare)
{
HashValue* ret = NULL;
HashNode* node = (HashNode*)BSTree_Get(hash, key, compare);//从树中获取节点
if( node != NULL )//如果有该节点
{
ret = node->value;//取得值
}
return ret;//返回值
}
int Hash_Count(Hash* hash)//定义获取数量函数
{
return BSTree_Count(hash);
}
main.c
#include <stdio.h>
#include <stdlib.h>
#include "Hash.h"
struct Student
{
char* id;
char* name;
int age;
};
int compare_id(HashKey* k1, HashKey* k2)//比较字符串
{
return strcmp((char*)k1, (char*)k2);
}
int main(int argc, char *argv[])
{
Hash* hash = Hash_Create();
struct Student s1 = {
"9001201", "Delphi", 30};
struct Student s2 = {
"0xABCDE", "Java", 20};
struct Student s3 = {
"koabc", "C++", 40};
struct Student s4 = {
"!@#$%^", "C#", 10};
struct Student s5 = {
"Python", "Python", 10};
struct Student* ps = NULL;
Hash_Add(hash, s1.id, &s1, compare_id);
Hash_Add(hash, s2.id, &s2, compare_id);
Hash_Add(hash, s3.id, &s3, compare_id);
Hash_Add(hash, s4.id, &s4, compare_id);
Hash_Add(hash, s5.id, &s5, compare_id);
ps = Hash_Get(hash, "koabc", compare_id);
printf("ID: %s\n", ps->id);
printf("Name: %s\n", ps->name);
printf("Age: %d\n", ps->age);
Hash_Destroy(hash);
return 0;
}
分析: