SlideShare a Scribd company logo
1 of 6
Download to read offline
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
#include
#include
using namespace std;
template
void printList(list list1);
template
list getElements(list L, list P);
int main(int argc, char **argv)
{
// Declare list variables
list list1;
list 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
void printList(list list1)
{
cout << " ";
// Use an iterator to walk the list. Print the value of
// for each node.
typename list::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
list getElements(list L, list P)
{
//A result list to return to the caller
list 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
#include
#include
using namespace std;
template
void printList(list list1);
template
list getElements(list L, list P);
int main(int argc, char **argv)
{
// Declare list variables
list list1;
list 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
void printList(list list1)
{
cout << " ";
// Use an iterator to walk the list. Print the value of
// for each node.
typename list::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
list getElements(list L, list P)
{
//A result list to return to the caller
list resultList;
// TODO: Implement the details for the function.
list::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::iterator itVal = L.begin();
std::advance(itVal, idx);//advance pointer
//store value in new list
resultList.push_back(*itVal);
}
return resultList;
}

More Related Content

Similar to Implement a function called getElements() that takes in two lists. T.pdf

Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docx
milissaccm
 
Stack linked list
Stack linked listStack linked list
Stack linked list
bhargav0077
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
ankit11134
 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
AdrianEBJKingr
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
ezzi97
 
QUESTION If you look at the code, youll see that we keep two list.pdf
QUESTION If you look at the code, youll see that we keep two list.pdfQUESTION If you look at the code, youll see that we keep two list.pdf
QUESTION If you look at the code, youll see that we keep two list.pdf
manjan6
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
EricvtJFraserr
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
aathiauto
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
rajkumarm401
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
aathmaproducts
 
2. (S points) Write a function to convert a list of strings into an i.pdf
 2. (S points) Write a function to convert a list of strings into an i.pdf 2. (S points) Write a function to convert a list of strings into an i.pdf
2. (S points) Write a function to convert a list of strings into an i.pdf
allwayscollection
 
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdfLab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
QalandarBux2
 

Similar to Implement a function called getElements() that takes in two lists. T.pdf (20)

Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 
Stacks,queues,linked-list
Stacks,queues,linked-listStacks,queues,linked-list
Stacks,queues,linked-list
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Although people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docxAlthough people may be very accustomed to reading and understanding .docx
Although people may be very accustomed to reading and understanding .docx
 
Stack linked list
Stack linked listStack linked list
Stack linked list
 
Please help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdfPlease help solve this in C++ So the program is working fin.pdf
Please help solve this in C++ So the program is working fin.pdf
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
 
Chapter 8 sorting algo
Chapter 8 sorting algoChapter 8 sorting algo
Chapter 8 sorting algo
 
Function Pointer
Function PointerFunction Pointer
Function Pointer
 
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
--INSTRUCTION- --It helps to first create if-then-else structure to fi.pdf
 
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdfHomework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
Homework 05 - Linked Lists (C++)(1) Implement the concepts of a un.pdf
 
QUESTION If you look at the code, youll see that we keep two list.pdf
QUESTION If you look at the code, youll see that we keep two list.pdfQUESTION If you look at the code, youll see that we keep two list.pdf
QUESTION If you look at the code, youll see that we keep two list.pdf
 
This assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdfThis assignment and the next (#5) involve design and development of a.pdf
This assignment and the next (#5) involve design and development of a.pdf
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docx
 
Need to be done in C++ Please Sorted number list implementation wit.pdf
Need to be done in C++  Please   Sorted number list implementation wit.pdfNeed to be done in C++  Please   Sorted number list implementation wit.pdf
Need to be done in C++ Please Sorted number list implementation wit.pdf
 
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdfComplete the provided partial C++ Linked List program. Main.cpp is g.pdf
Complete the provided partial C++ Linked List program. Main.cpp is g.pdf
 
Need to be done in C Please Sorted number list implementation with.pdf
Need to be done in C  Please   Sorted number list implementation with.pdfNeed to be done in C  Please   Sorted number list implementation with.pdf
Need to be done in C Please Sorted number list implementation with.pdf
 
Data structure lab manual
Data structure lab manualData structure lab manual
Data structure lab manual
 
2. (S points) Write a function to convert a list of strings into an i.pdf
 2. (S points) Write a function to convert a list of strings into an i.pdf 2. (S points) Write a function to convert a list of strings into an i.pdf
2. (S points) Write a function to convert a list of strings into an i.pdf
 
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdfLab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
Lab02kdfshdfgajhdfgajhdfgajhdfgjhadgfasjhdgfjhasdgfjh.pdf
 

More from arracollection

On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdfOn the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
arracollection
 
Discuss the relationship between gap rule gene mutations, pair-rule .pdf
Discuss the relationship between gap rule gene mutations, pair-rule .pdfDiscuss the relationship between gap rule gene mutations, pair-rule .pdf
Discuss the relationship between gap rule gene mutations, pair-rule .pdf
arracollection
 
Create a directory your virtual machine. Within that directory creat.pdf
Create a directory your virtual machine. Within that directory creat.pdfCreate a directory your virtual machine. Within that directory creat.pdf
Create a directory your virtual machine. Within that directory creat.pdf
arracollection
 
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdfBEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
arracollection
 

More from arracollection (20)

On April 18, 1906, an earthquake killed over 3,000 people when it hi.pdf
On April 18, 1906, an earthquake killed over 3,000 people when it hi.pdfOn April 18, 1906, an earthquake killed over 3,000 people when it hi.pdf
On April 18, 1906, an earthquake killed over 3,000 people when it hi.pdf
 
On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdfOn the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
On the planet of Caracas, in the Stellar Solorais Ka Chunk Galaxy, th.pdf
 
Muv is homozygous You have a C. elegans worm with the following geno.pdf
Muv is homozygous You have a C. elegans worm with the following geno.pdfMuv is homozygous You have a C. elegans worm with the following geno.pdf
Muv is homozygous You have a C. elegans worm with the following geno.pdf
 
Material Science (Solidification)What is the solute atoms concent.pdf
Material Science (Solidification)What is the solute atoms concent.pdfMaterial Science (Solidification)What is the solute atoms concent.pdf
Material Science (Solidification)What is the solute atoms concent.pdf
 
Let P[x] denote the vector space of all polynomials. Let E be the set.pdf
Let P[x] denote the vector space of all polynomials. Let E be the set.pdfLet P[x] denote the vector space of all polynomials. Let E be the set.pdf
Let P[x] denote the vector space of all polynomials. Let E be the set.pdf
 
Investment in Note ReceivableSuppliesPrepaid RentBuildingsAc.pdf
Investment in Note ReceivableSuppliesPrepaid RentBuildingsAc.pdfInvestment in Note ReceivableSuppliesPrepaid RentBuildingsAc.pdf
Investment in Note ReceivableSuppliesPrepaid RentBuildingsAc.pdf
 
In a tube containing 1 ml of a 15 dilution of A, and 4 ml of a 110.pdf
In a tube containing 1 ml of a 15 dilution of A, and 4 ml of a 110.pdfIn a tube containing 1 ml of a 15 dilution of A, and 4 ml of a 110.pdf
In a tube containing 1 ml of a 15 dilution of A, and 4 ml of a 110.pdf
 
gnmenttakeAssignmentMaindoinvoker assignmentstakeAssignmernt Dr. L. .pdf
gnmenttakeAssignmentMaindoinvoker assignmentstakeAssignmernt Dr. L. .pdfgnmenttakeAssignmentMaindoinvoker assignmentstakeAssignmernt Dr. L. .pdf
gnmenttakeAssignmentMaindoinvoker assignmentstakeAssignmernt Dr. L. .pdf
 
Every cell in our bodies expresses the same genes. True or False.pdf
Every cell in our bodies expresses the same genes. True or False.pdfEvery cell in our bodies expresses the same genes. True or False.pdf
Every cell in our bodies expresses the same genes. True or False.pdf
 
Describe the partition of Z resulting from the equivalence relati.pdf
Describe the partition of Z resulting from the equivalence relati.pdfDescribe the partition of Z resulting from the equivalence relati.pdf
Describe the partition of Z resulting from the equivalence relati.pdf
 
describe what is meant by Rh factor when typing blood.Solution.pdf
describe what is meant by Rh factor when typing blood.Solution.pdfdescribe what is meant by Rh factor when typing blood.Solution.pdf
describe what is meant by Rh factor when typing blood.Solution.pdf
 
Describe the causes, symptoms, and treatment of conjunctivitisSo.pdf
Describe the causes, symptoms, and treatment of conjunctivitisSo.pdfDescribe the causes, symptoms, and treatment of conjunctivitisSo.pdf
Describe the causes, symptoms, and treatment of conjunctivitisSo.pdf
 
Discuss the relationship between gap rule gene mutations, pair-rule .pdf
Discuss the relationship between gap rule gene mutations, pair-rule .pdfDiscuss the relationship between gap rule gene mutations, pair-rule .pdf
Discuss the relationship between gap rule gene mutations, pair-rule .pdf
 
Create a directory your virtual machine. Within that directory creat.pdf
Create a directory your virtual machine. Within that directory creat.pdfCreate a directory your virtual machine. Within that directory creat.pdf
Create a directory your virtual machine. Within that directory creat.pdf
 
Compare between the two light microscope SolutionFeature1. .pdf
Compare between the two light microscope  SolutionFeature1. .pdfCompare between the two light microscope  SolutionFeature1. .pdf
Compare between the two light microscope SolutionFeature1. .pdf
 
Chordin, Tolloid and Sizzled all play important roles in BMP signal r.pdf
Chordin, Tolloid and Sizzled all play important roles in BMP signal r.pdfChordin, Tolloid and Sizzled all play important roles in BMP signal r.pdf
Chordin, Tolloid and Sizzled all play important roles in BMP signal r.pdf
 
Chills are used in casting where the part is likely to form a cavity.pdf
Chills are used in casting where the part is likely to form a cavity.pdfChills are used in casting where the part is likely to form a cavity.pdf
Chills are used in casting where the part is likely to form a cavity.pdf
 
A drug that disrupts tubulin polymerzation is applied to some cell.pdf
A drug that disrupts tubulin polymerzation is applied to some cell.pdfA drug that disrupts tubulin polymerzation is applied to some cell.pdf
A drug that disrupts tubulin polymerzation is applied to some cell.pdf
 
At the second metaphase of meiosis Chromosomes have single chromatid.pdf
At the second metaphase of meiosis  Chromosomes have single chromatid.pdfAt the second metaphase of meiosis  Chromosomes have single chromatid.pdf
At the second metaphase of meiosis Chromosomes have single chromatid.pdf
 
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdfBEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
BEFO Ca.nin view shows longi tudimal vertical view or alde viu is a.pdf
 

Recently uploaded

Recently uploaded (20)

Philosophy of china and it's charactistics
Philosophy of china and it's charactisticsPhilosophy of china and it's charactistics
Philosophy of china and it's charactistics
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
latest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answerslatest AZ-104 Exam Questions and Answers
latest AZ-104 Exam Questions and Answers
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

Implement a function called getElements() that takes in two lists. T.pdf

  • 1. 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 #include #include using namespace std; template void printList(list list1); template list getElements(list L, list P); int main(int argc, char **argv)
  • 2. { // Declare list variables list list1; list 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 ** ";
  • 3. getchar(); return 0; } template void printList(list list1) { cout << " "; // Use an iterator to walk the list. Print the value of // for each node. typename list::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 list getElements(list L, list P) { //A result list to return to the caller list resultList; // TODO: Implement the details for the function.
  • 4. return resultList; } Solution /** * List.cpp - This program implements and tests the getIntersection * and getUnion functions. * * TODO: Include your name and course number here. */ #include #include #include using namespace std; template void printList(list list1); template list getElements(list L, list P); int main(int argc, char **argv) { // Declare list variables list list1; list 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");
  • 5. // 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 void printList(list list1) { cout << " "; // Use an iterator to walk the list. Print the value of // for each node. typename list::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
  • 6. list getElements(list L, list P) { //A result list to return to the caller list resultList; // TODO: Implement the details for the function. list::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::iterator itVal = L.begin(); std::advance(itVal, idx);//advance pointer //store value in new list resultList.push_back(*itVal); } return resultList; }