The code below will display the highest frequency that it can find in my hash table (from which is a bunch of link lists) 10 times. I need my code to print the top 10 frequencies in my hash table. I don’t know how to do this (code examples would be great, simple English logic / pseudo code is just as great).
- I am creating a temporary hash list called 'tmp' which points to my hashtable hash table
- Then the while loop goes through the list and searches for the highest frequency, which is int 'tmp-> freq'
- The loop will continue this process of duplicating the highest frequency it finds, with the variable 'topfreq', until it reaches the end of the linked lists in the hash table.
My 'node' is a structure consisting of the variables "freq" (int) and "word" (128 char). When the loop has nothing else to look for, it prints these two values on the screen.
The problem is that I cannot wrap my head in figuring out how to find the next lowest number from the number I just found (and this may include another node with the same frequency value, so I have to check that it is not same).
void toptenwords()
{
int topfreq = 0;
int minfreq = 0;
char topword[SIZEOFWORD];
for(int p = 0; p < 10; p++)
{
for(int m = 0; m < HASHTABLESIZE; m++)
{
node* tmp;
tmp = hashtable[m];
while(tmp != NULL)
{
if(tmp->freq > topfreq)
{
topfreq = tmp->freq;
strcpy(topword, tmp->word);
}
tmp = tmp->next;
}
}
cout << topfreq << "\t" << topword << endl;
}
}
Any help would be USED :)
Bobby source
share