document hash search

This commit is contained in:
Krishna Vedala 2020-05-28 20:24:44 -04:00
parent 969e916871
commit d9ffc8aca1
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,100 +1,139 @@
// Copyright 2020 Arctic2333
#include <stdlib.h>
#include<stdio.h>
#define MAX 6 // Determines how much data
# define HASHMAX 5 // Determines the length of the hash table
/** /**
* Hash Search Algorithm * \file
* Best Time Complexity (1) * \brief 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, * \copyright 2020 Arctic2333
* 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 * 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.
*
* @warning This program is only for educational purposes. It has serious flaws
* in implementation with regards to memory management resulting in large
* amounts of memory leaks.
* @todo fix the program for memory leaks and better structure in C++ and not C
* fashion
*/
#include <cstdlib>
#include <iostream>
#define MAX 6 ///< Determines how much data
#define HASHMAX 5 ///< Determines the length of the hash table
int data[MAX] = {1, 10, 15, 5, 8, 7}; //!< test data
/**
* a one-way linked list
*/
typedef struct list { typedef struct list {
int key; int key; //!< key value for node
struct list * next; struct list* next; //!< pointer to next link in the chain
} } node, /**< define node as one item list */
node, * link; *link; ///< pointer to nodes
node hashtab[HASHMAX];
int counter = 1; node hashtab[HASHMAX]; ///< array of nodes
/* int h(int key)
// int counter = 1;
/**
* Mode of hash detection : * Mode of hash detection :
* Division method */ * Division method
int h(int key) { * \param [in] key to hash
return key % HASHMAX; * \returns hash value for `key`
} */
/* void create_list(int key) int h(int key) { return key % HASHMAX; }
/**
* The same after the remainder will be added after the same hash header * The same after the remainder will be added after the same hash header
* To avoid conflict, zipper method is used * To avoid conflict, zipper method is used
* Insert elements into the linked list in the header */ * Insert elements into the linked list in the header
* \param [in] key key to add to list
* \warning dynamic memory allocated to `n` never gets freed.
* \todo fix memory leak
*/
void create_list(int key) { // Construct hash table void create_list(int key) { // Construct hash table
link p, n; link p, n;
int index; int index;
n = (link) malloc(sizeof(node)); n = (link)malloc(sizeof(node));
n -> key = key; n->key = key;
n -> next = NULL; n->next = NULL;
index = h(key); index = h(key);
p = hashtab[index].next; p = hashtab[index].next;
if (p != NULL) { if (p != NULL) {
n -> next = p; n->next = p;
hashtab[index].next = n; hashtab[index].next = n;
} else { } else {
hashtab[index].next = n; } 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. * Input the key to be searched, and get the hash header position through the H
* If found @return element depth and number of searches * (int key) function, then one-dimensional linear search. If found @return
* If not found @return -1 */ * element depth and number of searches If not found @return -1
int hash_search(int key) { // Hash lookup function */
int hash_search(int key, int* counter) { // Hash lookup function
link pointer; link pointer;
int index; int index;
counter = 0;
*counter = 0;
index = h(key); index = h(key);
pointer = hashtab[index].next; pointer = hashtab[index].next;
printf("data[%d]:", index);
std::cout << "data[" << index << "]:";
while (pointer != NULL) { while (pointer != NULL) {
counter++; counter[0]++;
printf("data[%d]:", pointer -> key); std::cout << "data[" << pointer->key << "]:";
if (pointer -> key == key) if (pointer->key == key)
return 1; return 1;
else else
pointer = pointer -> next; pointer = pointer->next;
} }
return 0; return 0;
} }
/** main function */
int main() { int main() {
link p; link p;
int key, index, i; // Key is the value to be found int key, index, i, counter; // Key is the value to be found
index = 0; index = 0;
// You can write the input mode here // You can write the input mode here
while (index < MAX) { // Construct hash table while (index < MAX) { // Construct hash table
create_list(data[index]); create_list(data[index]);
index++; index++;
} }
for (i = 0; i < HASHMAX; i++) { // Output hash table for (i = 0; i < HASHMAX; i++) { // Output hash table
printf("hashtab [%d]", i); std::cout << "hashtab [" << i << "]\n";
printf("\n");
p = hashtab[i].next; p = hashtab[i].next;
while (p != NULL) { while (p != NULL) {
printf("please int key:"); std::cout << "please int key:";
if (p -> key > 0) if (p->key > 0)
printf("[%d]", p -> key); std::cout << "[" << p->key << "]";
p = p -> next; p = p->next;
} }
printf("\n"); std::cout << std::endl;
} }
while (key != -1) { while (key != -1) {
// You can write the input mode here // You can write the input mode here
// test key = 10 // test key = 10
key = 10; key = 10;
if (hash_search(key)) if (hash_search(key, &counter))
printf("search time = %d\n", counter); std::cout << "search time = " << counter << std::endl;
else else
printf("no found!\n"); std::cout << "no found!\n";
key = -1; // Exit test key = -1; // Exit test
/* The test sample is returned as: data[0]:data[5]:data[15]:data[10]:search time = 3 /* The test sample is returned as:
* The search is successful. There are 10 in this set of data */ * 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; return 0;
} }