为什么这个函数没有返回,或者值卡住?

时间:2016-11-12 03:37:32

标签: c++ function templates linked-list

我编写了一个函数,将参数与链表中的值进行比较,并返回参数在链表中出现的数字,但它似乎无法正常工作,因为它在我调用时似乎卡住了功能。下面是我主要使用的一些代码主函数和程序卡住的函数,另外这里有一些示例输出link to output picture

#include "linkedList.h"
#include <iostream>

using namespace std;

int main()
{
int num;

linkedListType<int> list1;
cout << "Enter the numbers you wish to store on the list to stop enter -999:";
cin >> num;
while (num != -999)
{
    list1.insert(num);
    cin >> num;
}

list1.print();
cout << "\nEnter the number for whom you wish to know how many times it appears in the list\n";
cin >> num;
cout << "The number of times " << num << " appears in the lis is: " << list1.numberOfTimesItemAppears(num);

system("pause");
return 0;
}

//function in linkedListType


template <class Type>
int linkedListType<Type>::numberOfTimesItemAppears(const Type& searchItem)
{
     int count=0;
     nodeType<Type> *current; //pointer to traverse the list
     current = first; //set current to point to the first
     //node in the list

while (current != NULL) //search the list
{
    if (current->info == searchItem)
    {
        current = current->link;
        count++;
    }
}
return count;
} 

0 个答案:

没有答案