Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a method countValue() Implement a method countValue() that counts the

ID: 3852977 • Letter: I

Question

Implement a method countValue()

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); (40 points)

Generate 20 random numbers in the range of 0 to 4 (10points)

insert each number in the linked list (10 points)

Output the list by using a method which you would call writeLinkedList which you would add to the ListP.cpp (20 points)

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

Remember that all the above is to be included in the file ListP.cpp

Remember to upload ListP.cpp into the associated Drop box Module 3: Count List.

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

Explanation / Answer

Hi, Implemented the required functions and added a main to test them too, i have added comments to help you understand.

#include <iostream>

#include <list> // for list operations

using namespace std;

void writeLinkedList(list<int> l)//printing the list

{

for (list<int>::iterator i=l.begin(); i!=l.end(); i++)// using iterator to print the list

cout << *i << " ";

}

int countValue(list<int> front, const int item)// count the occurences of given item

{

int count=0;

for (list<int>::iterator i=front.begin(); i!=front.end(); i++)

{

if(*i==item)

count++;

}

return count;

}

list<int> populateLinkedlist()

{

std::list<int> l;

for(int i=0;i<20;i++) //pushing 20 random numbers

{

l.push_front( rand() % 5 ); // rand will give us random number and doing modulu 5 will map it to range 0-4

}

return l;

}

int main()

{

// declaring list

std::list<int> list1;

list1=populateLinkedlist();

writeLinkedList(list1); // printing the linkedlist

cout<<endl;

for(int i=0;i<=4;i++)

{

cout<<i<<":"<< countValue(list1,i);

if(i!=4)

cout<<",";

}

}

o/p for the above code is

1 2 1 0 1 3 4 0 2 2 1 4 2 1 0 3 0 2 1 3
0:4,1:6,2:5,3:3,4:2

Thumbs up if this was helpful, otherwise let me know in comments. Good day.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote