Hi everyone,
Since it's the weekend, I can't get assistance from my professor or his assistant until Monday.
My deadline for submission is approaching and I'd feel a lot safer getting as much work as I can.
I'm implementing a hash table to store strings using linear probing. My implementation results in my program hanging and I would like some insight as to why it's hanging. Any help would be appreciated.
I'm sure the problem lies within my linear probing function and/or the comparison of strings.
Omitting ! produces a result but the desired one.
If you have further questions pm or email at [email removed].
Preview of the HashDef.h
I've included all the files necessary to test any modification you might make.
Thank you in advance for any help as well as for your time.
#ifndef H_HashDef
#define H_HashDef
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>
#include <iomanip>
using namespace std;
class HtType
{
public:
HtType();
//default constructor
//initializes Size, the number
//of current elements in the table
void ReadData();
int hashFunction(const char *key, int keyLenght);
void LinearProbing();
void PrintData();
private:
int size;
string word[131];
const char *word2;
int len;
int hIndex;
int HTSIZE;
string hashTable[131];
int IndexStatusList[131];
};
HtType::HtType()
{
size = 0;
HTSIZE = 131;
for(int b = 0; b < HTSIZE;b++)
{
IndexStatusList[b] = 0;
}
for(int c = 0; c < HTSIZE; c++)
{
hashTable[c] = " ";
}
}
void HtType::ReadData()
{
ifstream DataIn;
DataIn.open("words.txt");
if(!DataIn)
{
cout <<"Error opening data file\n";
}
else
{
DataIn >> word[size];
size++;
while(!DataIn.eof())
{
DataIn >> word[size];
size++;
}
}
}
int HtType::hashFunction(const char *key, int keyLenght)
{
int sum = 0;
for(int j =0; j < keyLenght ; j++)
{
sum = sum + static_cast<int>(tolower(key[j]));
}
return sum = sum % HTSIZE;
}
void HtType::LinearProbing()
{
for(int a = 0; a < size; a++)
{
len = static_cast<int>(word[a].length());
word2 = word[a].data(); //copies string as a c-string argument
hIndex = hashFunction(word2,len);
//int i = 1;
if(IndexStatusList[hIndex] == 0)
{
hashTable[hIndex] = word[a];
IndexStatusList[hIndex] = 1;
}
else
{
int i = 1;
while(IndexStatusList[hIndex] != 0)
{
if(!word[a].compare(word2))
{
int addone = IndexStatusList[hIndex];
addone++;
IndexStatusList[hIndex] = addone;
}
else
{
hIndex = (hashFunction(word2,len) + i) % HTSIZE;
i++;
}
}
hashTable[hIndex] = word[a];
IndexStatusList[hIndex] = 1;
}
}
}
void HtType::PrintData()
{
for(int a = 0; a < HTSIZE; a++)
{
cout << hashTable[a]<< "\n";
}
}
#endif