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

using c++ ? Define a list object, and call a function to populate it using a loo

ID: 3709512 • Letter: U

Question

using c++

? Define a list object, and call a function to populate it using a loop as shown in 18-6.

? Call a function to display the list and output the number of nodes in the list.

? Then, call a function to add to the list the numbers (5, 15, 25, 35, 45, 55, 65, 75, 85) inserting them in numerical order using a loop, an iterator, list.begin, and list.insert.

? Then call the display function to display the list again and the number of nodes.

? Then reverse the list and display it again, and output the number of nodes.

? Then call a function that removes the ends of the list, and then redisplay the list and the number of nodes.

All output is to be done in the output function. The output function that displays the list must accept a string for the message prior to displaying the list and the number of nodes.

Sample output function declaration: void display_list(list &myList, string message);

Sample output function call: display_list(myList, "The original list is: ");

The program must use an iterator in the solution for adding the fives and for output.

Output Format and contents: The original list is: 0 10 20 30 40 50 60 70 80 90 The number of 1ist nodes is: 10 The list with the additional numbers is: 051015 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 The number of list nodes is now: 19 The list in reverse is: 90 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 10 5 0 The number of list nodes is: 19 The list with the ends removed: 85 80 75 70 65 60 55 50 45 40 35 30 25 20 15 105 The number of list nodes is: 17 Process returned 0 (0x0execution time 0.062 Press any key to continue

Explanation / Answer

#include <iostream>
#include <list>
#include <string>
#include <algorithm>
#include <iterator>
using namespace std;
list <int> :: iterator it;
void display_list(list<int> &myList , string s1) {

int num = 0;
string s2 = "The number of list nodes is : ";

  
cout << s1 << endl;
for (it = myList.begin(); it != myList.end(); ++it) {
cout << *it << " ";
num++;
}
cout << endl;
cout << s2 << num << endl << endl;
  
}

void addNum(list<int> &myList , list<int> arr) {
for (it = arr.begin(); it != arr.end(); ++it) {
myList.push_back(*it);
}

myList.sort();
}

int main() {
list <int> myList = { 0, 10, 20, 30, 40, 50, 60, 70, 80, 90};
display_list(myList, "The original list is:");
list<int> arr = {5, 15, 25, 35, 45, 55, 65, 75, 85};
addNum(myList, arr);
display_list(myList, "The list with the additional numbers is:");

myList.reverse();
display_list(myList, "The list in reverse is:");

myList.pop_front();
myList.pop_back();
display_list(myList, "The list with the ends removed:");

}