I was practicing implementing a hash function. I tried to use linear probing to insert in hash table. The insertion is working alright but I am not satisfied with the recursion. It doesn't feel like "good recursion". The Linear_Probing
function instead of being recursive itself uses two other functions (excluding hash function) for recursive insertion: Index_Finder()
and its auxiliary function.
Is there any way I can do the recursion better in this code for inserting into a hash table?
int Hash(int number); //<Hash function returns index by adding the first and last digit of a number;
bool Linear_Probing(int array[], bool flags [], int MAX_SIZE, int number, int &collisions); //<This function if possible inserts the number in the Hash Table
int Index_Finder(int number, bool flags[], int MAX_SIZE, int &collisions); //<This function finds the index where the number should be inserted. It returns -1 if no empty slot is found.
int Index_Finder(bool flags[], int MAX_SIZE, int index, int limit, int &collisions); //<This is an auxilary function
int main()
{
const int MAX_SIZE = 11;
int collisions = 0;
int array[MAX_SIZE];
bool flags[MAX_SIZE];
for (int i = 0; i < MAX_SIZE; i++)
{
array[i] = 0;
flags[i] = false;
}
//<Inserting numbers in hash table using linear probing
Linear_Probing(array, flags, MAX_SIZE, 67, collisions);
Linear_Probing(array, flags, MAX_SIZE, 19, collisions);
Linear_Probing(array, flags, MAX_SIZE, 3, collisions);
Linear_Probing(array, flags, MAX_SIZE, 808, collisions);
Linear_Probing(array, flags, MAX_SIZE, 337, collisions);
Linear_Probing(array, flags, MAX_SIZE, 1, collisions);
Linear_Probing(array, flags, MAX_SIZE, 86, collisions);
Linear_Probing(array, flags, MAX_SIZE, 38, collisions);
return 0;
}
bool Linear_Probing(int array[], bool flags[], int MAX_SIZE, int number, int &collisions)
{
int dummy_collisions = 0;
int index = Index_Finder(number, flags, MAX_SIZE, dummy_collisions);
if (index == -1)
return false;
else
{
collisions += dummy_collisions;
array[index] = number;
flags[index] = true;
return true;
}
}
int Index_Finder(int number, bool flags[], int MAX_SIZE, int &collisions)
{
int index = Hash(number);
if (index < MAX_SIZE && !flags[index])
return index;
if (index >= MAX_SIZE)
{
index = 0;
if (!flags[index])
return index;
else
collisions++;
}
else
{
collisions++;
}
int limit = index;
return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);
}
int Index_Finder(bool flags[], int MAX_SIZE, int index, int limit, int &collisions)
{
if (limit == index)
return -1;
if (index == MAX_SIZE)
index = 0;
if (!flags[index])
return index;
else
{
collisions++;
return Index_Finder(flags, MAX_SIZE, ++index, limit, collisions);
}
}
int Hash(int number)
{
int first_digit = 0, last_digit = 0;
bool first_time = true;
while (number > 0)
{
if (first_time)
{
first_digit = number % 10;
number -= (number % 10);
number /= 10;
first_time = false;
}
else
{
last_digit = number % 10;
number -= (number % 10);
number /= 10;
}
}
return (first_digit + last_digit);
}