seperate chaining, printing one duplicate string

closed account (2604izwU)
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:
	static const int  tableSize = 10;

	struct item
	{
		std::string word;
		int count;
		int id;
		item*next;
		item(){
			count =0;
		}
	};

	item* HashTable[tableSize];


This is the print function I am trying to fix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void hash::printAll()
{
	int num = 1;
	int countTemp = 0;
	std::string temp;
	for(int i = 0; i< tableSize; i++)
	{
	item* ptr = HashTable[i];
	freq(i);
	if(ptr->word !="empty")
	{
		
		while(ptr!= NULL)
		{
			temp = ptr->word;
			countTemp = ptr->count;
			
			ptr = ptr->next;
			if(ptr !=NULL)
			{
				if(ptr->word != temp)
				ptr = ptr->next;
			}
			
		}
		std::cout<<temp<<" "<<"..."<<std::endl;
	}
	
	}
}
Why are you placing duplicates inside your hash table apart from values that hash to the same bucket?

Also your function's name is printAll, doesn't this mean you should be printing everything inside the hashtable?

Assuming you have correctly implemented your seperate chaining method, then this code should work:

1
2
3
4
5
6
7
8
9
10
void hash::printAll()
{
	for(int i = 0; i < tableSize; i++)
	{
	    item* ptr = HashTable[i];
	    if (ptr != nullptr) {
                std::cout << ptr->word << "...\n";
	    }
	}
}
closed account (2604izwU)
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.
Last edited on
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
closed account (2604izwU)
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?
Last edited on
explain what you mean by ...
halojim wrote:
that way it is in memory?
closed account (2604izwU)
Nvm, got it. Thanks for trying to help :).
Last edited on
Topic archived. No new replies allowed.