15,942,710 members
See more:
I have been struggling with one of my homework questions for quite some time. I am attempting to implement a getLocation(hash1, x) method, which finds the location of the entry with key x. If such an entry does not exist, it returns the location where a new entry with key x should be stored. The question itself involves probing a Hash Map with double hashing. My attempt at implementing the method is pasted below, but something appears to be wrong. I was wondering if anyone would be able to help me correct my code. Much thanks in advance!

Java
```/**
* Searches for an entry with key equal to x (which is known to have primary
* hash value hash1), returning the index at which it was found, or returning
* -(a+1) where a is the index of the first empty or available slot that can
* be used to store a new such entry.  Uses secondary hashing function
* h'(k) = 7 - k mod 7, where the secondary prime factor 7 is the smallest
* prime less than the current capacity.
*
* @param hash1 the precalculated hash value of the given key
* @param x the key
*/
protected int getLocation(int hash1, K x)
{

int i = hash1;
int hash2 = 7 - x.hashCode() % 7;
int present = -1;

do
{
if (isAvailable(i))
{
if (present == -1)
{
present = i;
}

if (table[i] == null)
{
break;
}
}

else if (table[i].getKey().equals(x))
{
return i;
}

i = (i + hash2) % capacity;
}
while (i != hash1);

return -(present + hash2);
}```
Posted
Richard MacCutchan 20-Nov-15 5:41am
something appears to be wrong
And what would that something be?