From 6511946c3e8b9a4b689ed28a487de11050f13b17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Mon, 2 Dec 2019 11:01:18 +0800 Subject: [PATCH 1/9] hash_search --- search/hash_search.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 search/hash_search.cpp diff --git a/search/hash_search.cpp b/search/hash_search.cpp new file mode 100644 index 000000000..7ed7e5628 --- /dev/null +++ b/search/hash_search.cpp @@ -0,0 +1,82 @@ +// Copyright 2020 Arctic2333 +#include +#include +#define MAX 6 +# define HASHMAX 5 +int data[MAX] = { 12, 160, 219, 522, 725, 9997}; +typedef struct list { + int key; + struct list * next; +} +node, * link; +node hashtab[HASHMAX]; +int counter = 1; +int h(int key) { + return key % HASHMAX; +} +void create_list(int key) { + link p, n; + int index; + n = (link) malloc(sizeof(node)); + n -> key = key; + n -> next = NULL; + index = h(key); + p = hashtab[index].next; + if (p != NULL) { + n -> next = p; + hashtab[index].next = n; + } else + hashtab[index].next = n; +} +int hash_search(int key) { + link pointer; + int index; + counter = 0; + index = h(key); + pointer = hashtab[index].next; + printf("data[%d]:", index); + while (pointer != NULL) { + counter++; + printf("data[%d]:", pointer -> next); + if (pointer -> key == key) + return 1; + else + pointer = pointer -> next; + } + return 0; +} +int main() { + link p; + int key, index, i; + index = 0; + printf("input data:"); + for (i = 0; i < HASHMAX; i++) + scanf("%d", & data[i]); + printf("\n"); + while (index < MAX) { + create_list(data[index]); + index++; + } + for (i = 0; i < HASHMAX; i++) { + printf("hashtab [%d]", i); + printf("n"); + p = hashtab[i].next; + while (p != NULL) { + printf("please int key:"); + if (p -> key > 0) + printf("[%d]", p -> key); + p = p -> next; + } + printf("\n"); + } + while (key != -1) { + printf("please input data\n"); + scanf("%d", & key); + if (hash_search(key)) + printf("search time = %d\n", counter); + else + printf("no found!\n"); + + } + return 0; +} From eb5899ed15d09c88a883e0c184dcf9ef6f2817f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Mon, 2 Dec 2019 11:10:17 +0800 Subject: [PATCH 2/9] hash_search --- search/hash_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 7ed7e5628..6d591f376 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -25,7 +25,8 @@ void create_list(int key) { if (p != NULL) { n -> next = p; hashtab[index].next = n; - } else + } + else hashtab[index].next = n; } int hash_search(int key) { @@ -76,7 +77,6 @@ int main() { printf("search time = %d\n", counter); else printf("no found!\n"); - } return 0; } From 4d9470555f546f38d3c5b4a024ab88d133d27114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Mon, 2 Dec 2019 11:12:56 +0800 Subject: [PATCH 3/9] hash_search --- search/hash_search.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 6d591f376..cd122fe2c 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -25,8 +25,7 @@ void create_list(int key) { if (p != NULL) { n -> next = p; hashtab[index].next = n; - } - else + } else hashtab[index].next = n; } int hash_search(int key) { From 4f83abdb507eeebe5d1051c46d793b236da68ba2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Mon, 2 Dec 2019 11:16:03 +0800 Subject: [PATCH 4/9] hash_search --- search/hash_search.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index cd122fe2c..68c361384 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -25,8 +25,8 @@ void create_list(int key) { if (p != NULL) { n -> next = p; hashtab[index].next = n; - } else - hashtab[index].next = n; + } else { + hashtab[index].next = n; } } int hash_search(int key) { link pointer; From 3db7cefdd94bb7edfe6230f9b8d38ad2d92a452e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Mon, 2 Dec 2019 11:20:55 +0800 Subject: [PATCH 5/9] hash_search --- search/hash_search.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 68c361384..a89d8566b 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -3,6 +3,8 @@ #include #define MAX 6 # define HASHMAX 5 +// Hash Search Algorithm +// Best Time Complexity Ω(1) int data[MAX] = { 12, 160, 219, 522, 725, 9997}; typedef struct list { int key; From 1e261cb8fe955245eaeea6f35182cfa93c6e1c84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Tue, 3 Dec 2019 00:13:51 +0800 Subject: [PATCH 6/9] add function definitions --- search/hash_search.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index a89d8566b..6b0c0dbca 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -16,7 +16,10 @@ int counter = 1; int h(int key) { return key % HASHMAX; } -void create_list(int key) { +// In this algorithm, we use the method of division and reservation remainder to construct the hash function, +// and use the method of chain address to solve the conflict, that is, we link a chain list after the data, +// and store all the records whose keywords are synonyms in the same linear chain list. +void create_list(int key) { // Construct hash table link p, n; int index; n = (link) malloc(sizeof(node)); @@ -30,7 +33,7 @@ void create_list(int key) { } else { hashtab[index].next = n; } } -int hash_search(int key) { +int hash_search(int key) { // Hash lookup function link pointer; int index; counter = 0; @@ -55,11 +58,11 @@ int main() { for (i = 0; i < HASHMAX; i++) scanf("%d", & data[i]); printf("\n"); - while (index < MAX) { + while (index < MAX) { // Construct hash table create_list(data[index]); index++; } - for (i = 0; i < HASHMAX; i++) { + for (i = 0; i < HASHMAX; i++) { // Output hash table printf("hashtab [%d]", i); printf("n"); p = hashtab[i].next; From b9f65b81d73ce5a26e8d0a12a50853195ffa0762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Tue, 3 Dec 2019 00:16:30 +0800 Subject: [PATCH 7/9] add function definitions --- search/hash_search.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 6b0c0dbca..0428341b6 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -16,9 +16,9 @@ int counter = 1; int h(int key) { return key % HASHMAX; } -// In this algorithm, we use the method of division and reservation remainder to construct the hash function, -// and use the method of chain address to solve the conflict, that is, we link a chain list after the data, -// and store all the records whose keywords are synonyms in the same linear chain list. +/* In this algorithm, we use the method of division and reservation remainder to construct the hash function, + and use the method of chain address to solve the conflict, that is, we link a chain list after the data, + and store all the records whose keywords are synonyms in the same linear chain list. */ void create_list(int key) { // Construct hash table link p, n; int index; From 27c1e3958a7f24d2daf3abe0aa27ed9579a71145 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Wed, 4 Dec 2019 00:03:55 +0800 Subject: [PATCH 8/9] hash_search --- search/hash_search.cpp | 48 +++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 0428341b6..372f5863e 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -1,11 +1,15 @@ // Copyright 2020 Arctic2333 #include #include -#define MAX 6 -# define HASHMAX 5 -// Hash Search Algorithm -// Best Time Complexity Ω(1) -int data[MAX] = { 12, 160, 219, 522, 725, 9997}; +#define MAX 6 // Determines how much data +# define HASHMAX 5 // Determines the length of the hash table +/** + * Hash Search Algorithm + * Best Time Complexity Ω(1) + * In this algorithm, we use the method of division and reservation remainder to construct the hash function, + * and use the method of chain address to solve the conflict, that is, we link a chain list after the data, + * and store all the records whose keywords are synonyms in the same linear chain list. */ +int data[MAX] = { 1, 10, 15, 5, 8, 7}; // test data typedef struct list { int key; struct list * next; @@ -13,12 +17,16 @@ typedef struct list { node, * link; node hashtab[HASHMAX]; int counter = 1; +/* int h(int key) + * Mode of hash detection : + * Division method */ int h(int key) { return key % HASHMAX; } -/* In this algorithm, we use the method of division and reservation remainder to construct the hash function, - and use the method of chain address to solve the conflict, that is, we link a chain list after the data, - and store all the records whose keywords are synonyms in the same linear chain list. */ +/* void create_list(int key) + * The same after the remainder will be added after the same hash header + * To avoid conflict, zipper method is used + * Insert elements into the linked list in the header */ void create_list(int key) { // Construct hash table link p, n; int index; @@ -33,6 +41,11 @@ void create_list(int key) { // Construct hash table } else { hashtab[index].next = n; } } +/* int hash_search(int key) + * Input the key to be searched, and get the hash header position through the H (int key) function, + * then one-dimensional linear search. + * If found @return element depth and number of searches + * If not found @return -1 */ int hash_search(int key) { // Hash lookup function link pointer; int index; @@ -42,7 +55,7 @@ int hash_search(int key) { // Hash lookup function printf("data[%d]:", index); while (pointer != NULL) { counter++; - printf("data[%d]:", pointer -> next); + printf("data[%d]:", pointer -> key); if (pointer -> key == key) return 1; else @@ -52,19 +65,16 @@ int hash_search(int key) { // Hash lookup function } int main() { link p; - int key, index, i; + int key, index, i; // Key is the value to be found index = 0; - printf("input data:"); - for (i = 0; i < HASHMAX; i++) - scanf("%d", & data[i]); - printf("\n"); + // You can write the input mode here while (index < MAX) { // Construct hash table create_list(data[index]); index++; } for (i = 0; i < HASHMAX; i++) { // Output hash table printf("hashtab [%d]", i); - printf("n"); + printf("\n"); p = hashtab[i].next; while (p != NULL) { printf("please int key:"); @@ -75,12 +85,16 @@ int main() { printf("\n"); } while (key != -1) { - printf("please input data\n"); - scanf("%d", & key); + // You can write the input mode here + // test key = 10 + key = 10; if (hash_search(key)) printf("search time = %d\n", counter); else printf("no found!\n"); + key = -1; // Exit test + /* The test sample is returned as: data[0]:data[5]:data[15]:data[10]:search time = 3 + * The search is successful. There are 10 in this set of data */ } return 0; } From bbea5ad5aa969386a033bcf168b3cc08a02804a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E6=B7=9E=E4=B8=B0?= <50102735+Arctic2333@users.noreply.github.com> Date: Wed, 4 Dec 2019 00:07:31 +0800 Subject: [PATCH 9/9] hash_seaarch --- search/hash_search.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/search/hash_search.cpp b/search/hash_search.cpp index 372f5863e..94d87b58a 100644 --- a/search/hash_search.cpp +++ b/search/hash_search.cpp @@ -2,7 +2,7 @@ #include #include #define MAX 6 // Determines how much data -# define HASHMAX 5 // Determines the length of the hash table +# define HASHMAX 5 // Determines the length of the hash table /** * Hash Search Algorithm * Best Time Complexity Ω(1)