From 6950131671ef99c9e89814c88edee115b8fa94e1 Mon Sep 17 00:00:00 2001 From: MrCuihi <649249762@qq.cm> Date: Sun, 18 Jun 2017 18:25:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E2=80=9C=E5=BC=80=E6=94=BE?= =?UTF-8?q?=E5=AE=9A=E5=9D=80=E6=B3=95=E4=B9=8B=E7=BA=BF=E6=80=A7=E6=8E=A2?= =?UTF-8?q?=E6=B5=8B=E5=86=8D=E6=95=A3=E5=88=97=E2=80=9D=E8=A7=A3=E5=86=B3?= =?UTF-8?q?Hash=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2017-1/Mrcui/cconte9/Hash.c | 71 +++++++++++++++++++++++++++++ 2017-1/Mrcui/cconte9/Hash.h | 28 ++++++++++++ 2017-1/Mrcui/cconte9/TestOutput.txt | 31 +++++++++++++ 2017-1/Mrcui/cconte9/main.c | 46 +++++++++++++++++++ 4 files changed, 176 insertions(+) create mode 100644 2017-1/Mrcui/cconte9/Hash.c create mode 100644 2017-1/Mrcui/cconte9/Hash.h create mode 100644 2017-1/Mrcui/cconte9/TestOutput.txt create mode 100644 2017-1/Mrcui/cconte9/main.c diff --git a/2017-1/Mrcui/cconte9/Hash.c b/2017-1/Mrcui/cconte9/Hash.c new file mode 100644 index 00000000..ed518b82 --- /dev/null +++ b/2017-1/Mrcui/cconte9/Hash.c @@ -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;//查找成功 +} + diff --git a/2017-1/Mrcui/cconte9/Hash.h b/2017-1/Mrcui/cconte9/Hash.h new file mode 100644 index 00000000..a6e367b9 --- /dev/null +++ b/2017-1/Mrcui/cconte9/Hash.h @@ -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);//查找 diff --git a/2017-1/Mrcui/cconte9/TestOutput.txt b/2017-1/Mrcui/cconte9/TestOutput.txt new file mode 100644 index 00000000..cda08e16 --- /dev/null +++ b/2017-1/Mrcui/cconte9/TestOutput.txt @@ -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 \ No newline at end of file diff --git a/2017-1/Mrcui/cconte9/main.c b/2017-1/Mrcui/cconte9/main.c new file mode 100644 index 00000000..7d541b02 --- /dev/null +++ b/2017-1/Mrcui/cconte9/main.c @@ -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; +}