Implement a function called getElements that takes in two li

Implement a function called getElements() that takes in two lists. The first list L consists of any type of items and the second list P consists of a list of integers. The implemented function will return a list consisting of the element in L that are in positions indicated by P. For example, if P=1, 7, 3, then the element in position 1, 7 and 3 in the list L must be returned. The function must be implemented using the C++ list SLT API. Hint: The implementation for this function will require some thought. Here are some hints for one approach to solve the problem. Use a for-loop to walk through list P in a similar way as done in the printList() function. Consider creating an index variable to track the item number in list L. Also, set the value of the iterator equal to a variable in this loop. For example: int index = 0; // Track the position int position = (*itr) ; // Get the item in P Create a second loop embedded within the first loop to walk through the elements in list L. If the index equals position-1 then you know you have found the element. Thus, call the push_back() function on the resultList to add the element. At the end of this loop increment the index variable by 1. Note that your iterator variable for the second loop must be named differently than the first loop. For example you can all it itr2. Output: The output from the program once the function is implement will be:

/**
* List.cpp - This program implements and tests the getIntersection
* and getUnion functions.
*
* TODO: Include your name and course number here.
*/

#include <iostream>
#include <cstdio>
#include <list>

using namespace std;

template <class Object>
void printList(list<Object> list1);

template <class Object>
list<Object> getElements(list<Object> L, list<int> P);

int main(int argc, char **argv)
{
   // Declare list variables
   list<string> list1;
   list<int> list2;

   // Add data to list 1
   list1.push_back(\"the\");
   list1.push_back(\"quick\");
   list1.push_back(\"brown\");
   list1.push_back(\"fox\");
   list1.push_back(\"jumped\");
   list1.push_back(\"over\");
   list1.push_back(\"the\");
   list1.push_back(\"lazy\");
   list1.push_back(\"dog\");
  
   // Add data to list 2
   list2.push_back(4);
   list2.push_back(3);
   list2.push_back(9);
   list2.push_back(6);
   list2.push_back(7);

   // Print out the lists
   cout << \"List 1: \" << endl;
   printList(list1);

   cout << \"List 2: \" << endl;
   printList(list2);

   cout << endl;

   cout << \"Results of getElements():\" << endl;
   printList(getElements(list1, list2));

   cout << \"\ ** Press any key to continue **\ \";
   getchar();

   return 0;
}

template <class Object>
void printList(list<Object> list1)
{
   cout << \" \";

   // Use an iterator to walk the list. Print the value of
   // for each node.
   typename list<Object>::iterator itr;
   for (itr = list1.begin(); itr != list1.end(); itr++)
   {
// Get the contents of each node
Object value = (*itr);

// Print the value of the node
cout << \" \" << value;
   }

   cout << endl;

   return;
}

template <class Object>
list<Object> getElements(list<Object> L, list<int> P)
{
   //A result list to return to the caller
   list<Object> resultList;

   // TODO: Implement the details for the function.
  


   return resultList;
}

Solution

/**
* List.cpp - This program implements and tests the getIntersection
* and getUnion functions.
*
* TODO: Include your name and course number here.
*/

#include <iostream>
#include <cstdio>
#include <list>

using namespace std;

template <class Object>
void printList(list<Object> list1);

template <class Object>
list<Object> getElements(list<Object> L, list<int> P);

int main(int argc, char **argv)
{
// Declare list variables
list<string> list1;
list<int> list2;

// Add data to list 1
list1.push_back(\"the\");
list1.push_back(\"quick\");
list1.push_back(\"brown\");
list1.push_back(\"fox\");
list1.push_back(\"jumped\");
list1.push_back(\"over\");
list1.push_back(\"the\");
list1.push_back(\"lazy\");
list1.push_back(\"dog\");
  
// Add data to list 2
list2.push_back(4);
list2.push_back(3);
list2.push_back(9);
list2.push_back(6);
list2.push_back(7);

// Print out the lists
cout << \"List 1: \" << endl;
printList(list1);

cout << \"List 2: \" << endl;
printList(list2);

cout << endl;

cout << \"Results of getElements():\" << endl;
printList(getElements(list1, list2));

cout << \"\ ** Press any key to continue **\ \";
getchar();

return 0;
}

template <class Object>
void printList(list<Object> list1)
{
cout << \" \";

// Use an iterator to walk the list. Print the value of
// for each node.
typename list<Object>::iterator itr;
for (itr = list1.begin(); itr != list1.end(); itr++)
{
// Get the contents of each node
Object value = (*itr);

// Print the value of the node
cout << \" \" << value;
}

cout << endl;

return;
}

template <class Object>
list<Object> getElements(list<Object> L, list<int> P)
{
//A result list to return to the caller
list<Object> resultList;

// TODO: Implement the details for the function.
list<int>::iterator itr;
for (itr = P.begin(); itr != P.end(); itr++)
{
   // Get the contents of each node
   int idx = (*itr);
  
   if(idx >= L.size())   //check if index is greater than list size
       continue;
  
   typename std::list<Object>::iterator itVal = L.begin();      
std::advance(itVal, idx);//advance pointer

   //store value in new list
   resultList.push_back(*itVal);
}


return resultList;
}

Implement a function called getElements() that takes in two lists. The first list L consists of any type of items and the second list P consists of a list of in
Implement a function called getElements() that takes in two lists. The first list L consists of any type of items and the second list P consists of a list of in
Implement a function called getElements() that takes in two lists. The first list L consists of any type of items and the second list P consists of a list of in
Implement a function called getElements() that takes in two lists. The first list L consists of any type of items and the second list P consists of a list of in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site