下面的代码将打印它在我的哈希表(其中是一堆链表)中可以找到的最高频率 10 次。我需要我的代码来打印我的哈希表中的前 10 个频率。我不知道该怎么做(代码示例会很棒,简单的英语逻辑/伪代码也很棒)。
- 我创建了一个名为“tmp”的临时哈希列表,它指向我的哈希表“hashtable”
- 然后一个 while 循环遍历列表并查找最高频率,即 int 'tmp->freq'
- 循环将继续使用变量“topfreq”复制它找到的最高频率的过程,直到它到达哈希表上链表的末尾。
我的“节点”是一个由变量“freq”(int)和“word”(128 个字符)组成的结构。当循环没有其他内容可搜索时,它会在屏幕上打印这两个值。
问题是,我无法弄清楚如何从我刚刚找到的数字中找到下一个最小的数字(这可能包括另一个具有相同频率值的节点,所以我必须检查这个词也不一样)。
void toptenwords()
{
int topfreq = 0;
int minfreq = 0;
char topword[SIZEOFWORD];
for(int p = 0; p < 10; p++) // We need the top 10 frequencies... so we do this 10 times
{
for(int m = 0; m < HASHTABLESIZE; m++) // Go through the entire hast table
{
node* tmp;
tmp = hashtable[m];
while(tmp != NULL) // Walk through the entire linked list
{
if(tmp->freq > topfreq) // If the freqency on hand is larger that the one found, store...
{
topfreq = tmp->freq;
strcpy(topword, tmp->word);
}
tmp = tmp->next;
}
}
cout << topfreq << "\t" << topword << endl;
}
}
任何和所有的帮助将不胜感激:)