I don't know if I'm missing something very simple but I'm having trouble initializing all values of my empty hash table to -1. I have an another array with the ID numbers (that I will hash). I'm initializing all values to -1 because I can check later if the value of my hash array is -1 then I can insert without quadratically probing.
In my constructor, I initialize the table size. Then I create an array with that size. Then I point my private pointer to that array so I'll always have access to it. From there, I initialize my table... And I have a question about the for loop in my constructor:
- Does it matter if i do arr[i] = -1 or table[i] = -1? Because table points to all of the indices right? and arr[] obviously can access it's own indices. So I don't see why either way it matters.
If somebody could enlighten me that would be great. Thanks.
HashTable::HashTable(int bsize)
{
this->tableSize= bsize;
int arr[bsize]; //creating an array to hold hash values
table = arr; //table pointer points to our new array
for(int i = 0; i < bsize; i++){
table[i] = -1;
}
}
void HashTable:: printTable(){
for(int i = 0; i < tableSize; i++){
cout << table[i] << endl;
}
}
Here's my class
class HashTable
{
int tableSize = 40009; // No. of buckets
// Pointer to an array containing buckets
int *table;
int numOfcolision =0;
public:
HashTable(int bsize); // Constructor
// inserts a key into hash table
bool insert(int key);
// hash function to map values to key
unsigned int hashFunction(int key);
void printTable();
int getNumOfCollision();
int search(int key);
};