哈希表简介:
哈希表是通过关键值来访问的数据结构,也就是说他通过把关键值映射到表中的一个位置来访问记录,以加快 查找速度,这个映射关系就是哈希函数,存放数据的列表叫做散列表。
首先给出实现哈希表的头文件
#ifndef __HASHTABLE_H__
#define __HASHTABLE_H__
#include<stdio.h>
#include<assert.h>
#include<malloc.h>
#include<string.h>
#include"Common.h"
typedef int DataType;
typedef void (*explore)(int * ,int );
enum State//每个位置都有自己的状态
{
EXIST,//存在的
EMPTY,//可用的
DELETE//被删除的
};
typedef struct KVP
{
DataType value;//存储的值
enum State state;//位置的状态
int order;//顺序
}KVP;
typedef struct HashTable
{
KVP *array;
int size;//有效容量
int capacity;//容量
int total;//已用容量(包括EXIST,DELETE)
explore exp;//函数指针
}HashTable;
//初始化
void InitHashTable(HashTable* ph,explore exp,DataType capacity);
//哈希函数
int HashFunc(int capacity,DataType data);
//插入
void InsertHashTable(HashTable* ph,DataType data);
//打印
void Print(HashTable* ph);
//查找
int Find(HashTable* ph,DataType data);
//删除
void DeleteHashTable(HashTable* ph,DataType data);
//判空
int EmptyHashTable(HashTable* ph);
//有效元素个数
int SizeHashTable(HashTable* ph);
//线性探索
void linear(int* hashaddr,int capacity);
//二次探索
void doub(int* hashaddr,int capacity);
//字符哈希函数
static size_t BKDRHash(const char * str);
//交换数组指针
void swap(HashTable *NewHt,HashTable * ph);
//销毁哈希表
void DestroyHashTable (HashTable * ph);
//增容
void AddCapacity(HashTable *ph);
#endif //__HASHTABLE_H__
//每个位置都有自己的状态
{
EXIST,