
Patrick B. answered 01/03/21
Math and computer tutor/teacher
Technically, you can get away with using the hash function x/10 + i^2 where i=0, which is simply
just using the ten's digit as the bucket. Basically you have a stem and leaf plot where the 10's place
is the stem...
Unpopulated buckets are guaranteed for this particular data with i=0 and no further iterations required.
using namespace std;
#include <iostream>
#include <vector>
vector<int> hashTable[10];
inline hash(int x,int i)
{
return(
(x/10+i*i)%10
);
}
int main()
{
int X[] = {24,42,34,62,73};
for (int x=0; x<5; x++)
{
for (int i=0; i<10; i++)
{
int _x = X[x];
int hashIndex = hash(_x,i);
if (hashTable[hashIndex].empty() )
{
cout << _x << " inserted at " << hashIndex << endl;
hashTable[hashIndex].push_back(_x);
break;
}
}
}
}