I'm having trouble finishing this hashing function that needs to use linear and quadratic probing, as well as double hashing within a switch statement. I've tried to write out the cases in the switch statement with no luck. I've seen pseudo code for hashing but I'm not sure how to correctly implement it in this case. I need help writing the hash code with the probes.
Here is my function:
void Hash(int *dta, int n, int &counter, int *&HashTable, int &TableSize, int swch)
{
// dta is the numerical data to hash
// n is the amount of data in dta
// counter is the variable you use for counting probes and stores
// HashTable is the variable you use as the hash table
// TableSize is the variable you use for the TableSize
// swch is either 0 or 1, depending on whether you are doing linear or quadratic probing
// The shell calls this function twice for every data set. The first time, swch will be zero (linear probing)
// the second time it will be 1 for quadratic probing. Write your code to handle this.
// compute TableSize (prime number > 2 * n)
TableSize = ComputeNearestPrime(n); //computes nearest prime closest but greater than 2 * n
try
{
HashTable = new int [TableSize];
if(HashTable == NULL)throw "allocation error";
}
catch (...)
{
cout << "Table allocation error - out of memory" << endl;
}
for(int i = 0; i < TableSize; i++)
HashTable[i] = -1; // initialize
counter = 0;
int R = ComputeNearestPrime(TableSize/2);
switch (swch)
{
//0 is linear
//1 is quadratic
//2 is double
case 0:
for (int i = 0; i < n; i++)
{
//counter += ...;
}
//case 1:
//case 2:
}
return;
}
Aucun commentaire:
Enregistrer un commentaire