I am seeking a review of a previous project I completed to help understand hash tables.
The following is my implementation of a hash-table using linear probing.
My solution assumes a number of variables have been initialised:
- Private int[] H; Representing the hash table with each value initialised to -1
- private int hSize; Storing the size of the hash table
Hash Function:
public int hash(int key){
return key % hSize;
}
Add value to hash table:
private void addToHash(int value){
int i;
for (i = hash(value); H[i] != -1; i = (i + 1) % H.length)
if (H[i] == value) return;
H[i] = value;
}
Search for value:
public int hashSearch(int key){
int hash = hash(key);
for (int i = hash; H[i] != -1; i = (i + 1) % H.length)
if (H[i] == key) return i;
return -1;
}
I am looking for feedback regarding the design of my solution / programming style - Anything which could be improved in the future or would not be encouraged when programming in the industry.