TheAlgorithms-C-Plus-Plus/search/hash_search.cpp

152 lines
3.8 KiB
C++
Raw Normal View History

2019-12-04 00:03:55 +08:00
/**
2020-05-29 08:24:44 +08:00
* \file
* \brief Hash Search Algorithm - Best Time Complexity (1)
*
* \copyright 2020 Arctic2333
*
* 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
{
2020-05-29 08:24:44 +08:00
int key; //!< key value for node
struct list* next; //!< pointer to next link in the chain
} node, /**< define node as one item list */
*link; ///< pointer to nodes
node hashtab[HASHMAX]; ///< array of nodes
// int counter = 1;
/**
2019-12-04 00:03:55 +08:00
* Mode of hash detection :
2020-05-29 08:24:44 +08:00
* Division method
* \param [in] key to hash
* \returns hash value for `key`
*/
int h(int key) { return key % HASHMAX; }
/**
2019-12-04 00:03:55 +08:00
* The same after the remainder will be added after the same hash header
* To avoid conflict, zipper method is used
2020-05-29 08:24:44 +08:00
* 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
2019-12-02 11:01:18 +08:00
link p, n;
int index;
2020-05-29 08:24:44 +08:00
n = (link)malloc(sizeof(node));
n->key = key;
n->next = NULL;
2019-12-02 11:01:18 +08:00
index = h(key);
p = hashtab[index].next;
if (p != NULL)
{
2020-05-29 08:24:44 +08:00
n->next = p;
2019-12-02 11:01:18 +08:00
hashtab[index].next = n;
}
else
{
2020-05-29 08:24:44 +08:00
hashtab[index].next = n;
}
2019-12-02 11:01:18 +08:00
}
2020-05-29 08:24:44 +08:00
/**
* 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, int* counter)
{ // Hash lookup function
2019-12-02 11:01:18 +08:00
link pointer;
int index;
2020-05-29 08:24:44 +08:00
*counter = 0;
2019-12-02 11:01:18 +08:00
index = h(key);
pointer = hashtab[index].next;
2020-05-29 08:24:44 +08:00
std::cout << "data[" << index << "]:";
while (pointer != NULL)
{
2020-05-29 08:24:44 +08:00
counter[0]++;
std::cout << "data[" << pointer->key << "]:";
if (pointer->key == key)
2019-12-02 11:01:18 +08:00
return 1;
else
2020-05-29 08:24:44 +08:00
pointer = pointer->next;
2019-12-02 11:01:18 +08:00
}
2020-05-29 08:24:44 +08:00
2019-12-02 11:01:18 +08:00
return 0;
}
2020-05-29 08:24:44 +08:00
/** main function */
int main()
{
2019-12-02 11:01:18 +08:00
link p;
2020-05-29 08:24:44 +08:00
int key, index, i, counter; // Key is the value to be found
2019-12-02 11:01:18 +08:00
index = 0;
2020-05-29 08:24:44 +08:00
2019-12-04 00:03:55 +08:00
// You can write the input mode here
while (index < MAX)
{ // Construct hash table
2019-12-02 11:01:18 +08:00
create_list(data[index]);
index++;
}
2020-05-29 08:24:44 +08:00
for (i = 0; i < HASHMAX; i++)
{ // Output hash table
2020-05-29 08:24:44 +08:00
std::cout << "hashtab [" << i << "]\n";
2019-12-02 11:01:18 +08:00
p = hashtab[i].next;
2020-05-29 08:24:44 +08:00
while (p != NULL)
{
2020-05-29 08:24:44 +08:00
std::cout << "please int key:";
if (p->key > 0)
std::cout << "[" << p->key << "]";
p = p->next;
2019-12-02 11:01:18 +08:00
}
2020-05-29 08:24:44 +08:00
std::cout << std::endl;
2019-12-02 11:01:18 +08:00
}
2020-05-29 08:24:44 +08:00
while (key != -1)
{
2019-12-04 00:03:55 +08:00
// You can write the input mode here
// test key = 10
key = 10;
2020-05-29 08:24:44 +08:00
if (hash_search(key, &counter))
std::cout << "search time = " << counter << std::endl;
2019-12-02 11:01:18 +08:00
else
2020-05-29 08:24:44 +08:00
std::cout << "no found!\n";
2019-12-04 00:03:55 +08:00
key = -1; // Exit test
2020-05-29 08:24:44 +08:00
/* 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 */
2019-12-02 11:01:18 +08:00
}
2020-05-29 08:24:44 +08:00
2019-12-02 11:01:18 +08:00
return 0;
}