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] 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; +}