c++ - 链接列表中的频率

标签 c++ list frequency

我正在尝试创建一个名为 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/

相关文章:

python - 3D NumPy 数组中每个子数组或切片的频率计数

c++ - 递归函数返回的字符串是否超出范围?

python - 我使用 python3.5 和 OpenCV 3.1.0,OpenCV 函数 cv2.countNonZero(img) 我得到一个错误

Python将数组的值拆分为不同的列

通过理解将 Python 列表转换为字典

python 函数之间的单个整型变量

python - 从声音文件中检测频率

c++ - 通过套接字发送 HBITMAP

c++ - 为什么 std::move() 没有 _Remove_reference 就不能工作?

arduino - Arduino能否以1-4 kHz的微秒采样音频?