雜湊表開放定址法demo
阿新 • • 發佈:2020-12-20
技術標籤:資料結構與演算法
#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;
}