Click here to Skip to main content
15,942,710 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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!

 * 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
 * @return index of found entry or if not found
protected int getLocation(int hash1, K x) 

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

        if (isAvailable(i)) 
            if (present == -1) 
                present = i;               

            if (table[i] == null) 

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

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

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900