I was given an assignment to create a hash table that contains 30 buckets (20 primary, and 10 overflow), with each bucket containing 3 slots (each slot containing 2 stings for key and data passed in), a counter integer and a pointer variable that points to the next overflow bucket. My last C++ class was over a year ago so I'm completely lost as to how I am supposed to create this table properly (with no help from my professor).
This is my class declaration below. It technically compiles, however it crashes immediately, and when it debugs, I get an "Access Violation Reading Location" error stating "this was nullptr" on my home computer (and it includes a specific memory location on the computer I used in class).
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// Define Global Variables
#define MAXBUCKETs 30
#define MAXSLOTs 3
typedef char STR10[10 + 1];
typedef char STR20[20 + 1];
// Define Class
class hash{
public:
int primaryBuckets = 20;
int overflowBuckets = 10;
union bucket
{
int count;
bucket* nextOverflow;
struct slot {
string keyValue;
string dataValue;
};
slot* slots[2];
} HashTable[30];
hash(); // Initialize HashTable
int HashFunction(STR10 key, int buckets); // Hash key to index
void InsertIntoHT(STR10 key, STR20 data); // Add data to slot
void PrintItemsInIndex();
void InsertOverflow(STR10 key, STR20 data, int index);
void InsertPrimary(STR10 key, STR20 data, int index);
};
My constructor, from what I understand, initialized the array in the right order
::hash::hash()
{
for (int i = 0; i < MAXBUCKETs; i++)
{
HashTable[i].count = 0;
for (int j = 0; j < MAXSLOTs; j++)
{
HashTable[i].slots[j]->keyValue = "no_data";
HashTable[i].slots[j]->dataValue = "no_data";
}
}
}
I'm pretty desperate for help at this point. I'm almost certain it's a bad pointer, but I've never understood them well. All help will be greatly appreciated! (This is also my first post so hopefully I didn't do anything wrong)