patterncppMinor
Recursive Insertion in Hash Table (Linear Probing)
Viewed 0 times
insertionlinearprobinghashrecursivetable
Problem
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
Is there any way I can do the recursion better in this code for inserting into a hash table?
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); //= 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);
}Solution
-
When commenting to describe a function's purpose, it's more common to have them on top of the signature of the function definition. Having them next to the prototypes like that makes those lines harder to read and increases the code'd horizontal length.
-
Your curly brace usage with single-line statements is inconsistent. It's also better to use them for all statements like these and can prevent some bugs and ease maintenance.
-
Instead of initializing the arrays with a loop, you can use
While it may not matter in smaller programs, it's still more concise and can be safer.
-
You're passing C-arrays to functions, which shouldn't be done in C++ as this will cause them to decay to pointers. Instead, pass in an object of a container class such as
When commenting to describe a function's purpose, it's more common to have them on top of the signature of the function definition. Having them next to the prototypes like that makes those lines harder to read and increases the code'd horizontal length.
-
Your curly brace usage with single-line statements is inconsistent. It's also better to use them for all statements like these and can prevent some bugs and ease maintenance.
-
Instead of initializing the arrays with a loop, you can use
std::fill_n():std::fill_n(array, MAX_SIZE, 0);
std::fill_n(flags, MAX_SIZE, false);While it may not matter in smaller programs, it's still more concise and can be safer.
-
You're passing C-arrays to functions, which shouldn't be done in C++ as this will cause them to decay to pointers. Instead, pass in an object of a container class such as
std::vector (dynamic) or std::array (static). Choose whichever best gets the job done for you.Code Snippets
std::fill_n(array, MAX_SIZE, 0);
std::fill_n(flags, MAX_SIZE, false);Context
StackExchange Code Review Q#74935, answer score: 6
Revisions (0)
No revisions yet.