Implement one of the hashing procedures that we have discuss
Implement one of the hashing procedures that we have discussed, and the following-related functions: INSERT (item) DELETE (item) FIND (item) Make sure that your program correctly handles collisions and full hash table!
Solution
/** insert **/
public void insert(String key, String val)
{
int tmp = hash(key);
int i = tmp;
do
{
if (keys[i] == null)
{
keys[i] = key;
vals[i] = val;
currentSize++;
return;
}
if (keys[i].equals(key))
{
vals[i] = val;
return;
}
i = (i + 1) % maxSize;
} while (i != tmp);
}
/** delete **/
public void remove(String key)
{
if (!contains(key))
return;
int i = hash(key);
while (!key.equals(keys[i]))
i = (i + 1) % maxSize;
keys[i] = vals[i] = null;
currentSize--;
}
/** find **/
private int getBucketIndex(K key)
{
int hashCode = key.hashCode();
int index = hashCode % numBuckets;
return index;
}
