哈希表-散列表
- 键(key):组,的编号
- 值(value):组,的其他信息
- 索引:数组的下标,用来索引和定位元素
- 哈希桶:存储,组,索引值中存储相同元素
- 哈希函数:组中的元素,映射到索引上
/*哈希表元素定义*/
typedef struct _ListNode{
struct _ListNode* next;
int key;
void* data;
}ListNode;
typedef ListNode* List;
typedef ListNode* Element;
/*哈希表结构定义*/
typedef struct _HashTable{
int TableSize;
List* Thelists;
}HashTable;
/*初始化哈希表*/
HashTable* InitHash(int TableSize)
{
HashTable* hTable = NULL;
if (TableSize <= 0) {
TableSize = DEFAULT_SIZE;
}
hTable = (HashTable*)malloc(sizeof(HashTable));
if (NULL == hTable)
{
printf("HashTable malloc error.\n");
return NULL;
}
hTable->TableSize = TableSize;
//为Hash 桶分配内存空间,其为一个指针数组
hTable->Thelists = (List*)malloc(sizeof(List) * TableSize);
if (NULL == hTable->Thelists){
printf("HashTable malloc error\n");
free(hTable);
return NULL;
}
//为Hash 桶对应的指针数组初始化链表节点
for (int i = 0; i < TableSize; i++) {
hTable->Thelists[i] = (ListNode*)malloc(sizeof(ListNode));
if (NULL == hTable->Thelists[i]) {
printf("HashTable malloc error\n");
free(hTable->Thelists);
free(hTable);
return NULL;
} else {
memset(hTable->Thelists[i], 0, sizeof(ListNode));
}
}
return hTable;
}
HashTable* InitHash(int TableSize)
:函数返回值,结构体类型(参数:哈希表的大小)HashTable* hTable = NULL;
:定义哈希表指针,值设置(NULL)
if (TableSize <= 0) {
TableSize = DEFAULT_SIZE;
}
注解:如果传递参数
TableSize
小于等于0,设置TableSize
默认值16
hTable = (HashTable*)malloc(sizeof(HashTable));
if (NULL == hTable)
{
printf("HashTable malloc error.\n");
return NULL;
}
注解:分配内存到
hTable
,如果内存分配失败,返回空(NULL)
hTable->TableSize = TableSize;
//为Hash 桶分配内存空间,其为一个指针数组
hTable->Thelists = (List*)malloc(sizeof(List) * TableSize);
if (NULL == hTable->Thelists){
printf("HashTable malloc error\n");
free(hTable);
return NULL;
}
注解:确定哈希表的大小,给哈希桶分配内存空间。如果内存分配失败,释放
hTable
,返回(NULL)
//为Hash 桶对应的指针数组初始化链表节点
for (i = 0; i < TableSize; i++) {
hTable->Thelists[i] = (ListNode*)malloc(sizeof(ListNode));
if (NULL == hTable->Thelists[i]) {
printf("HashTable malloc error\n");
free(hTable->Thelists);
free(hTable);
return NULL;
} else {
memset(hTable->Thelists[i], 0, sizeof(ListNode));
}
}
注解:
TableSize
大小,每组分配ListNode
内存
头文件代码页
hash_table.h
#pragma once
#define DEFAULT_SIZE 16
/*哈希表元素定义*/
typedef struct _ListNode{
struct _ListNode* next;
int key;
void* data;
}ListNode;
typedef ListNode* List;
typedef ListNode* Element;
/*哈希表结构定义*/
typedef struct _HashTable{
int TableSize;
List* Thelists;
}HashTable;
/*哈希函数*/
//int Hash(void* key, int TableSize);
int Hash(int key, int TableSize);
/*初始化哈希表*/
HashTable* InitHash(int TableSize);
/*哈希表插入*/
void Insert(HashTable* HashTable, int key, void* value);
/*哈希表查找*/
Element Find(HashTable* HashTable, int key);
/*哈希表销毁*/
void Destory(HashTable* HashTable);
/*哈希表元素中提取数据*/
void* Retrieve(Element e);
源文件代码页
hash.cpp
#include
#include
#include
#include "hash_table.h"
/*根据key 计算索引,定位Hash 桶的位置*/
int Hash(int key, int TableSize)
{
return (key % TableSize);
}
/*初始化哈希表*/
HashTable* InitHash(int TableSize)
{
int i = 0;
HashTable* hTable = NULL;
if (TableSize <= 0) {
TableSize = DEFAULT_SIZE;
}
hTable = (HashTable*)malloc(sizeof(HashTable));
if (NULL == hTable)
{
printf("HashTable malloc error.\n");
return NULL;
}
hTable->TableSize = TableSize;
//为Hash 桶分配内存空间,其为一个指针数组
hTable->Thelists = (List*)malloc(sizeof(List) * TableSize);
if (NULL == hTable->Thelists){
printf("HashTable malloc error\n");
free(hTable);
return NULL;
}
//为Hash 桶对应的指针数组初始化链表节点
for (i = 0; i < TableSize; i++) {
hTable->Thelists[i] = (ListNode*)malloc(sizeof(ListNode));
if (NULL == hTable->Thelists[i]) {
printf("HashTable malloc error\n");
free(hTable->Thelists);
free(hTable);
return NULL;
} else {
memset(hTable->Thelists[i], 0, sizeof(ListNode));
}
}
return hTable;
}
/*从哈希表中根据键值查找元素*/
Element Find(HashTable* HashTable, int key)
{
int i = 0;
List L = NULL;
Element e = NULL;
i = Hash(key, HashTable->TableSize);
L = HashTable->Thelists[i];
e = L->next;
while (e != NULL && e->key != key)
e = e->next;
return e;
}
/*哈希表插入元素,元素为键值对*/
void Insert(HashTable* HashTable, int key,void* value)
{
Element e = NULL, tmp = NULL;
List L = NULL;
e = Find(HashTable, key);
if (NULL == e)
{
tmp = (Element)malloc(sizeof(ListNode));
if (NULL == tmp)
{
printf("malloc error\n");
return;
}
L = HashTable->Thelists[Hash(key, HashTable->TableSize)];
tmp->data = value;
tmp->key = key;
tmp->next = L->next;
L->next = tmp;
}
else
printf("the key already exist\n");
}
/*哈希表删除元素,元素为键值对*/
void Delete(HashTable* HashTable, int key)
{
Element e = NULL, last = NULL;
List L = NULL;
int i = Hash(key, HashTable->TableSize);
L = HashTable->Thelists[i];
last = L;
e = L->next;
while (e != NULL && e->key != key) {
last = e;
e = e->next;
}
if (e) {//如果键值对存在
last->next = e->next;
delete(e);
}
}
/*哈希表元素中提取数据*/
void* Retrieve(Element e){
return e ? e->data : NULL;
}
/*销毁哈希表*/
void Destory(HashTable* HashTable){
int i = 0;
List L = NULL;
Element cur = NULL, next = NULL;
for (i = 0; i < HashTable->TableSize; i++)
{
L = HashTable->Thelists[i];
cur = L->next;
while (cur != NULL)
{
next = cur->next;
free(cur);
cur = next;
}
free(L);
}
free(HashTable->Thelists);
free(HashTable);
}
void main(void){
char* elems[] = { "翠花","小芳","王老师" };
int i = 0;
HashTable* HashTable;
HashTable = InitHash(31);
Insert(HashTable, 1, elems[0]);
Insert(HashTable, 2, elems[1]);
Insert(HashTable, 3, elems[2]);
Delete(HashTable, 1);
for (i = 0; i < 4; i++) {
Element e = Find(HashTable, i);
if (e) {
printf("%s\n", (const char*)Retrieve(e));
} else {
printf("Not found [key:%d]\n", i);
}
}
system("pause");
}