Assume that keys are inserted using linear probing and that keys are never deleted. The hash function is hash(int key, int tsize), which returns a hash value between zero and (tsize-1).
Below are four implementations of hash_find():
(a)
int hash_find(int *table,
int tsize, int key)
{
int h;
h = hash(key, tsize);
while (table[h] != key) h++;
return h;
}
|
(b)
int hash_find(int *table,
int tsize, int key)
{
int h, i, j;
i = 0;
h = hash(key, tsize);
while(i < tsize) {
j = table[(h+i)%tsize];
if (j == key) return (h+i)%tsize;
if (j == 0) return -1;
i++;
}
}
|
(c)
int hash_find(int *table,
int tsize, int key)
{
int h, j, i;
i = 0;
h = hash(key, tsize);
while(i < tsize) {
j = (h+i)%tsize;
if (j == key) return table[j];
if (j == 0) return table[-1];
i++;
}
}
|
(d)
int hash_find(int *table,
int tsize, int key)
{
int h, j, i;
i = 0;
h = hash(key, tsize);
j = h;
while(i < tsize) {
if (j > tsize) j -= tsize;
if (hash(table[j], tsize) == h) return j;
if (table[j] == 0) return -1;
i++;
}
}
|