I have a hash table that reads words from an input file, converts each word to a hash value, and stores each word to the hash value index of the hash table. The collisions are handled by separate chaining. I am having trouble figuring out the logic needed to print only one of the duplicated words in a bucket linked list that contains duplicates or even a different word that has the same hashvalue. Sorry for the train wreck but I have been going at this for a while with no success.
any insight would be appreciated
This is the node struct
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class hash
{
private:
staticconstint tableSize = 10;
struct item
{
std::string word;
int count;
int id;
item*next;
item(){
count =0;
}
};
item* HashTable[tableSize];
I am reading from a text file with multiple words and there are some duplicates, my end goal is to print out each word once and its frequency in the text file. The name was from earlier when I was checking the indexes to make sure it was chaining, just never changed the name.
What you should do is when ever you are inserting into a bucket, iterate through the bucket and make sure they item does not already exist in that bucket before inserting it. Otherwise, you will need search through the list again in order to see if the word has been used.
So you have 2 choices, either do the hard work during insertion and get it over with or do it every time you are printing the table
So basically I have to do what I have been trying unsuccessfully to do but in the insertion function? Wouldn't it be better doing it in the print function? that way it is in memory?