#include "HashTable.h"
/***************************************************************
* @file HashTable.c
* @brief 初始散列表
* @author txj
* @version v1
* @date 2020/10/18
**************************************************************/
Status InitHashTable(tHashTable *H)
{
int i;
H->Count = 0;
H->elem = (int *)malloc(sizeof(int)*HASHSIZE);/*申请m个大小为int的内存空间*/
for (i = 0; i < HASHSIZE; i++)
{
H->elem[i] = NULLKEY;/*往申请的内存空间里赋初值*/
}
return OK;
}
/***************************************************************
* @file HashTable.c
* @brief 散列函数(除留余数法)
* @author txj
* @version v1
* @date 2020/10/18
**************************************************************/
int Hash(int key)
{
return key % HASHSIZE;/*根据key值在申请的内存空间里找到合适的位置进行插入*/
}
/***************************************************************
* @file HashTable.c
* @brief 插入操作
* @author txj
* @version v1
* @date 2020/10/18
**************************************************************/
void InsertHash(tHashTable *H, int key)
{
int addr = Hash(key); /*求散列地址*/
while (H->elem[addr]!=NULLKEY)/*当前位置不等于NULLKEY就可以直接插入*/
{
addr = (addr + 1) % HASHSIZE; /*使用开放定址法解决冲突*/
}
H->elem[addr] = key;/*如果当前位置不等于NULLKEY,说明当前位置没有数据插入*/
H->Count++;
}
/***************************************************************
* @file HashTable.c
* @brief 散列表查找数据
* @author txj
* @version v1
* @date 2020/10/18
**************************************************************/
Status SearchHash(tHashTable *H, int key, int *addr)
{
*addr = Hash(key);
while (H->elem[*addr]!=key)
{
*addr = (*addr + 1) % HASHSIZE;/*往下一个地址继续查找*/
if (H->elem[*addr] == NULLKEY || *addr == Hash(key))
{
return UNSUCCESS; /*若是找不到数据或者循环回到源点,查找失败*/
}
}
return SUCCESS;
}
/***************************************************************
* @file HashTable.c
* @brief 散列表遍历
* @author txj
* @version v1
* @date 2020/10/18
**************************************************************/
void Show_HashTable(tHashTable *H)
{
int index = 0;
for(index = 0;index < HASHSIZE; index++)
{
printf("H->elem[%d] = %d\n",index,H->elem[index]);
}
}
#ifndef _HASHTABLE_H
#define _HASHTABLE_H
#include<stdlib.h>
#include<stdio.h>
#define SUCCESS 1
#define UNSUCCESS 0
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
#define HASHSIZE 12 /*定义初始散列表长为数组的长度*/
#define NULLKEY -32768
typedef int Status;
/*开放定址法哈希表数据结构*/
typedef struct HashTable
{
int *elem; /*数据元素存储基址*/
int Count; /*当前数据元素个数*/
}tHashTable;
/*函数声明*/
Status InitHashTable(tHashTable *H);
int Hash(int key);
void InsertHash(tHashTable *H, int key);
Status SearchHash(tHashTable *H, int key, int *addr);
void Show_HashTable(tHashTable *H);
/*散列函数:
(1)直接定址法
(2)数字分析法(取关键字)
(3)平方取中法
(4)折叠法
(5)除留余数法
(6)随机数法
冲突解决:
(1)开放定址法(线性探测法),二次探测法(双向),随机探测法
(2)再散列函数法(采取不同的散列函数)
(3)链地址法(链表)
(4)公共溢出区法
一个基本表,一个溢出表
*/
#endif
#include"HashTable.h"
int main(void)
{
int index = 0 addr = 0 ret = 0;;
int key[12] = {
1,5,8,17,20};
tHashTable H;
/*哈希表初始化*/
InitHashTable(&H);
/*插入元素*/
for(index = 0;index < 5;index++)
{
InsertHash(&H,key[index]);
}
/*查找元素*/
ret = SearchHash(&H, key[0], &addr);
if(ret == SUCCESS)
{
printf("成功查找到了元素\n");
}
/*遍历元素*/
Show_HashTable(&H);
return 0;
}