3 Replies - 1690 Views - Last Post: 15 November 2011 - 06:30 PM Rate Topic: -----

#1 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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;
//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)
{
	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

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



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  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#3 HKCHaN92  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#4 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1132
  • View blog
  • Posts: 2,490
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1