# Counting number of times an item occurs in a linked list

Page 1 of 1

## 3 Replies - 2795 Views - Last Post: 15 November 2011 - 06:30 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=255821&amp;s=3243dc04733546cbd50da780675c6780&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 HKCHaN92

Reputation: 2
• Posts: 85
• Joined: 23-July 11

# Counting number of times an item occurs in a linked list

Posted 15 November 2011 - 05:15 PM

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<int> 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

Here's what I have:
#include<iostream>
#include<list>
#include<tchar.h>

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

using namespace std;

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;
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)
{
for(list<int>::iterator i = front.begin(); i != front.end(); i++)
{
cout << *i << endl;
}
}

here are my 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

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

My professor provided the code for everything except the countValue function

Is This A Good Question/Topic? 0

## Replies To: Counting number of times an item occurs in a linked list

### #2 HKCHaN92

Reputation: 2
• Posts: 85
• Joined: 23-July 11

## Re: Counting number of times an item occurs in a linked list

Posted 15 November 2011 - 05:44 PM

btw I'm not done with the countValue function yet I want to get these errors taken care of before I finish it

### #3 HKCHaN92

Reputation: 2
• Posts: 85
• Joined: 23-July 11

## Re: Counting number of times an item occurs in a linked list

Posted 15 November 2011 - 06:21 PM

Also just noticed that I used using namespace std; twice and have corrected that

### #4 blackcompe

• D.I.C Lover

Reputation: 1158
• Posts: 2,538
• Joined: 05-May 05

## Re: Counting number of times an item occurs in a linked list

Posted 15 November 2011 - 06:30 PM

Since the list class is belongs to the std name space you need to specify that your using the name space before making any references to the list class.