我正在尝试创建一个名为 frequency 的 fn,它返回链表中给定整数的频率。 到目前为止:
int IntSLList::frequency(int e)
{
int total = 0;
IntSLLNode *temp;
for (temp = head; temp!=0 && !(temp-> info ==e) ; temp = temp->next)
{
total++;
}
return total;
但是它返回的数字(应该返回 1)却返回 8 我的链表有 10 个元素 (0-9
最佳答案
检查是否等于 e 应该在 for 循环体中完成。
int IntSLList::frequency(int e)
{
int total = 0;
IntSLLNode *temp;
for (temp = head; temp!=0 ; temp = temp->next)
{
if( temp->info == e )
total++;
}
return total;
}
关于c++ - 链接列表中的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35852334/