Spring-_- Bear 2022-01-26 12:01:01 阅读数:634
Hash Search algorithm design
Input 10 individual 0-99 Integer between , structure hash surface . The hash function is constructed by division and residue method , The way to deal with conflict is linear detection . notes : The test data must be in conflict
/* hash Search algorithm design : Input 10 individual 0-99 Integer between , structure hash surface . hash Function self design , The way to deal with conflict is linear detection . Write a program to realize . notes : The test data must be in conflict . */
#include<iostream>
#include <iomanip>
using namespace std;
int main()
{
int hash[10], number, item, i;
cout << " Please enter 10 individual 0-99 Integer between !" << endl;
for (i = 1; i <= 10; i++){
cout << " The first "<<i<< " Number :" ; // Input prompt
cin >> number;
// Judge whether the input data meets the requirements , If it does not meet the requirements, continue to enter
if (!(number >= 0 && number <= 99)){
cout << " The data you entered is incorrect , Please re-enter !!!" << endl;
i--;
continue;
}
// Find the remainder of the input number
item = number % 10;
while (true)
{
if (!(hash[item] <= 99 && hash[item] >= 0)){
// If conflict
hash[item] = number;
break;
}
else if (++item == 10) // Hash table to the end , Start from scratch
{
item = 0;
}
}
}
cout << " The generated hash table is as follows :" << endl;
for (i = 0; i < 10; i++)
{
cout << setw(4) << i << "|";
}
cout << endl << " _________________________________________________" << endl;
for (i = 0; i < 10; i++)
{
cout << setw(4) << hash[i] << "|"; //hash Table generation completed
}
return 0;
}
copyright:author[Spring-_- Bear],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/01/202201261200584313.html