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

Implement a method countValue() that counts the number of times an item occurs i

ID: 3545378 • Letter: I

Question

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.



The below code needs to go in ListP.cpp. It is missing the actual code of the functions countValue() and writeLinkedList():

#include <list>

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

void writeLinkedList(list<int> front);

using namespace std;
int main(){
list<int> front;
int listCount;
cout << "Enter the size of the list: ";
cin >> listCount;
for (int i = 1; i <= listCount; i++)
front.push_back(rand()%5);
cout << "Original List of Values:    "  << endl;
writeLinkedList(front);
cout << endl;
for(int j=0;j<5;++j)
cout << "j = " << j << " : " << countValue (front,j) << endl;
cout << endl;
return 0;
}


I have about got this solved I need help with the writelinkedList part of it. Thanks for your help.

Explanation / Answer

#include <iostream>

#include <list>

using namespace std;


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

void writeLinkedList(list<int> front);


int main(){

list<int> front;

int listCount;

cout << "Enter the size of the list: ";

cin >> listCount;

for (int i = 1; i <= listCount; i++)

front.push_back(rand()%5);

cout << "Original List of Values: " << endl;

writeLinkedList(front);

cout << endl;

for(int j=0;j<5;++j)

cout << "j = " << j << " : " << countValue (front,j) << endl;

cout << endl;

return 0;

}


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

{

int count = 0;

list<int>::iterator it = front.begin();

for (; it != front.end(); ++it)

if (item == *it)

count++;

return count;

}


void writeLinkedList(list<int> front)

{

list<int>::iterator it = front.begin();

for (; it != front.end(); ++it)

cout << (*it) << " ";

cout << endl;

}

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