如何在链表中查找多次出现的整数

时间:2014-10-08 03:38:50

标签: linked-list integer find-occurrences

我在找出一个可以帮助我在链表中​​找到多个整数的算法时遇到了麻烦

1 个答案:

答案 0 :(得分:0)

希望这有帮助:

#include <list>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
list<int> mylist;

mylist.push_back(1);
mylist.push_back(4);
mylist.push_back(1);
mylist.push_back(3);
mylist.push_back(4);
mylist.push_back(5);

list<int>::iterator iter;
for( iter = mylist.begin(); iter != mylist.end(); iter++)
{
    cout << *iter << endl;
}



for(list<int>::iterator it = mylist.begin(); it != mylist.end(); it++)
{

    list<int>::iterator finder = std::find(mylist.begin(), mylist.end(), *it);
    while   (finder != mylist.end())
    {

        finder = std::find(++finder, mylist.end(), *it);
        if(finder != mylist.end())
        {

            cout << "catch it" << endl;
            cout << * finder;
        }
    }

}
return 0;
}