Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“开放定址法之线性探测再散列”解决Hash冲突算法实现 #192

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions 2017-1/Mrcui/cconte9/Hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include "Hash.h"



Status Init(HashTable hashTable, int hashsize)//初始化散列表
{
int i;
Hash_size = hashsize;
hashTable->elem = (ElemType *) malloc(Hash_size * sizeof(ElemType));
hashTable->count = Hash_size;
for (i = 0; i < Hash_size; i++) {
hashTable->elem[i].key = nullval;
hashTable->elem[i].val = nullval;
}
return OK;
}


Status Hash(int data)//获得哈希函数
{
return data % Hash_size;
}


void Insert(HashTable hashTable, int hkey, int value)//插入关键字和相应的值
{
int hashaddress = Hash(value); //求哈希地址
int count = 0;//记录冲突次数
while (hashTable->elem[hashaddress].key != nullval)//发生冲突
{

hashaddress = (++hashaddress) % Hash_size;//利用开放定址的线性探测法解决冲突
count++;
}


hashTable->elem[hashaddress].key = hkey;
hashTable->elem[hashaddress].val = value;
}

void Display(HashTable hashTable)//打印结果
{
int i;
printf("--Hashtable--\n");
for (i = 0; i < hashTable->count; i++) {
if (hashTable->elem[i].val == nullval) {
printf("{[%d]:%d->%d}\n", i, -1, 0);
}
printf("{[%d]:%d->%d}\n", i, hashTable->elem[i].key, hashTable->elem[i].val);
}

printf("-------------\n");
}

Status Search(HashTable hashTable, int data)//查找
{
int hashaddress = Hash(data);


while (hashTable->elem[hashaddress].key != data) {

hashaddress = (++hashaddress) % Hash_size;

if (hashTable->elem[hashaddress].key == nullval || hashaddress == Hash(data))
return -1;
}


return hashaddress;//查找成功
}

28 changes: 28 additions & 0 deletions 2017-1/Mrcui/cconte9/Hash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "stdio.h"
#include "stdlib.h"
#include "time.h"

#define OK 1
#define nullval -33333

typedef int Status;
typedef int KeyType;
typedef int ValueType;
typedef struct _ElemType {
KeyType key; // 关键字
ValueType val; // 值
#ifdef CHAINED_HASH
struct _ElemType *next;
#endif
} ElemType;
typedef struct {
ElemType *elem; // 数据元素存储地址
int count; //数据元素个数
} Hash_Table, *HashTable;

int Hash_size = 0; //散列表表长
Status Init(HashTable hashTable, int hashsize);//初始化散列表
Status Hash(int data);//获得哈希函数
void Insert(HashTable hashTable, int hkey, int value);//插入关键字和相应的值
void Display(HashTable hashTable);//打印结果
Status Search(HashTable hashTable, int data);//查找
31 changes: 31 additions & 0 deletions 2017-1/Mrcui/cconte9/TestOutput.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/Users/cclin/Library/Caches/CLion2016.2/cmake/generated/untitled22-2bd8c90f/2bd8c90f/Debug/untitled22
--Hashtable--
{[0]:41->13}
{[1]:62->20}
{[2]:49->16}
{[3]:48->16}
{[4]:52->18}
{[5]:54->17}
{[6]:56->20}
{[7]:26->7}
{[8]:59->20}
{[9]:32->9}
{[10]:29->10}
{[11]:34->10}
{[12]:38->11}
{[13]:43->12}
-------------

------------测试查找用例-----------
[32] [33] [33] [32] [35] [36] [36] [37]
------------测试查找用例-----------
关键字为[32]的值在哈希表中的位置是:[9]
没有找到!
没有找到!
关键字为[32]的值在哈希表中的位置是:[9]
没有找到!
没有找到!
没有找到!
没有找到!

Process finished with exit code 0
46 changes: 46 additions & 0 deletions 2017-1/Mrcui/cconte9/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Hash.h"



int main() {
int i, j, result;
Hash_Table hashTable;
int number1 = 0, number2 = 0, number3 = 0;
srand(time(NULL));
int HASHSIZE = rand() % 5 + 13; // 定义散列表长为数组的长度
ValueType value[HASHSIZE];
KeyType key[HASHSIZE];
int seed = rand() % 3 + 6;
int test[seed];//测试查找用例
for (j = 0; j < HASHSIZE; j++) {
value[j] = rand() % 3 + 7 + number1;
number1++;
}
for (j = 0; j < HASHSIZE; j++) {
key[j] = rand() % 6 + 23 + number2;
number2 = number2 + 3;

}
for (j = 0; j < seed; j++) {
test[j] = rand() % 5 + 28 + number3;
number3++;
}
Init(&hashTable, HASHSIZE);//初始化哈希表
for (i = 0; i < HASHSIZE; i++) {
Insert(&hashTable, key[i], value[i]);//插入数据
}
Display(&hashTable);
printf("\n------------测试查找用例-----------\n");
for (j = 0; j < seed; j++) {
printf("[%d] ", test[j]);
}
printf("\n------------测试查找用例-----------\n");
for (j = 0; j < seed; j++) {
result = Search(&hashTable, test[j]);//查找数据
if (result == -1)
printf("没有找到!\n");
else printf("关键字为[%d]的值在哈希表中的位置是:[%d]\n", test[j], result);
}

return 0;
}