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