I am using a dynamic array of linked lists as a data structure to help avoid collisions in a hash table. I want to dynamically allocate the size of the array of linked lists, but I dont know how to initialize them both, for example, if you wanted a dynamically allocated array, I could just say
1 2
char * array;
array = newchar[50];
however, what I have now looks something like this:
1 2
private:
node ** hasht;
and in the constructor for that class, I have this:
1 2 3 4 5 6 7 8 9
hashtable::hashtable(int size = 13)
{
hasht = new node * [SIZE];
for(int i = 0; i < size; ++i;)
{
hasht[i] = NULL;
}
table_size = SIZE;
}
so i define hasht as a new node, just like i normally would, but then i still have int size pointing to my node. How do i define that as an array size, or does this constructor already do that?
Your hash table stores data of some type I'll call bucket_type in an array of buckets I'll call bucket_list_type. And the hash table is an array of pointers to these bucket lists I'll call hash_table_type.
If your hash table were a template class, parameterised by the bucket_type, using standard containersm a definition would look like: