Counting number of times an item occurs in a linked list

Posted by HanaCHaN92 on Stack Overflow See other posts from Stack Overflow or by HanaCHaN92
Published on 2011-11-16T01:32:52Z Indexed on 2011/11/16 1:51 UTC
Read the original article Hit count: 163

Filed under:
|
|
|

Here is the assignment: Here's the assignment: Implement a method countValue() that counts the number of times an item occurs in a linked list. Remember to use the STL list.

int countValue(list front, const int item);

Generate 20 random numbers in the range of 0 to 4, and insert each number in the linked list. Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp.

In a loop, call the method countValue() , and display the number of occurrences of each value from 0 to 4 in the list.

Remember that all the above is to be included in the file ListP.ccp Run: 2 3 4 0 1 0 2 4 2 3 3 4 3 3 3 0 0 2 0 2 0 : 5, 1 : 1, 2 : 5, 3 : 6, 4 : 3

and here is what I have so far:

#include<iostream>
#include<list>
#include<tchar.h>

int countValue(list<int> front, const int item);

using namespace std;
int _tmain(int argc, _TCHAR* argv[]){
list<int> front;
int listCount;
cout << "Enter the size of the list: ";
cin >> listCount;
for (int i = 1; i <= listCount; i++)
front.insert(rand()%5);
cout << "Original List of Values: " << endl;
//writeLinkedList(front, " ");
cout << endl;
for(int j=0;j<5;++j)
cout << countValue (front,j) << endl;
cout << endl;
return 0;
}

int countValue(list<int> front, const int item)
{   int count0;
    int count1;
    int count2;
    int count3;
    int count4;
    list<int> *List;
    for(list<int>::iterator i = front.begin(); i != front.end(); i++)
    {
        if(List->item == 0)
        {
            count0++;
        }
        if(List->item == 1)
        {
            count1++;
        }
        if(List->item == 2)
        {
            count2++;
        }
        if(List->item == 3)
        {
            count2++;
        }if(List->item == 4)
        {
            count4++;
        }
    }
}

And here are the errors:

error C2065: 'list' : undeclared identifier line 5
error C2062: type 'int' unexpected line 5
error C2661: 'std::list<_Ty>::insert' : no overloaded function takes 1 arguments line 16
error C3861: 'countValue': identifier not found line 21
IntelliSense: no instance of overloaded function "std::list<_Ty, _Ax>::insert [with _Ty=int, _Ax=std::allocator<int>]" matches the argument list line 16
IntelliSense: too few arguments in function call line 16
error C2039: 'item': is not a member of 'std::list<_Ty>' lines 34, 38, 42, 46, 49
IntelliSense: declaration is incompatible with "int countValue" (declared at line 5) line 25
IntelliSense: class "std::list<int, std:: allocator<int>>" has no member "item" lines 34, 38, 42, 46, 49

I just want to know what I've done wrong and how to fix it and also if someone could help me figure out if I'm doing the countValue function wrong or not based on the instructions I would really appreciate it. I've read the chapter in our textbook several times, looked up tutorials on youtube and on Dream in Code, and still I can not figure this out. All helpful information is appreciated!

© Stack Overflow or respective owner

Related posts about c++

Related posts about homework