Using inheritance, extend the template class indexList (posted on UR Courses) wi
ID: 2246431 • Letter: U
Question
Using inheritance, extend the template class indexList (posted on UR Courses) witih the following functions: e Concatenate two indexed lists. . Sort an indexed list using the selection sort algorithm (declared in indexList but not Find the position of a target using the binary search algorithm. Write a driver program that will create two indexed lists of integers, concatenate the two lists, sort the resuing list and then displayi. In addition e driver will search for a target in the resulting list A sample run follows. List 1 of type "int" Enter nunber of list itens: 4 Enter next item: 11 Enter next item: 7 Enter next item: 5 Enter next item: 34 List 2 of type -int" Enter nunber of list itens: 3 Enter next item: 0 Enter next item: 2 Enter next item: 49 List 3: List 1 concatenated to List 2 11, 7, 5, 34, 0, 2, 49 List 3 sorted in ascending order 0, 2, 5, 7, 11, 34, 49 Enter the number to search in List 3: 11 11 is at position 5 1. The header, implementation and driver programs should be respectively named: IndexList.h IndexList.cpp, ExtIndexList.h, ExtIndexList.cpp and TestExtIndexList.cpp.Explanation / Answer
Source code: This code have been written in c++ which asks the user to enter the number of elements in both list1 and list2 and it sorts the lists in ascending order and search for the user desired element.
#include <iostream>
#include <list>
#include <algorithm>
bool mycomparison (int first, int second)
{ return ( int(first)<int(second) ); }
int main ()
{
std::list<int> first, second;
int list1,list2;
int list,llist,ele;
std::cout << "Enter how many elements you want to insert in List1:";
std::cin>>list1;
for (int i=0; i<list1; i++){
std::cout << "Enter elements into List1:";
std::cin>>list;
first.push_back (list);
}
std::cout << "Enter how many elements you want to insert in List2:";
std::cin>>list2;
for (int i=0; i<list2; i++){
std::cout << "Enter elements into List2:";
std::cin>>llist;
second.push_back (llist);
}
first.sort();
second.sort();
first.merge(second);
std::cout << "List3: List1 concatenated to List2:";
for (std::list<int>::iterator it=first.begin(); it!=first.end(); ++it)
std::cout << ' ' << *it;
std::cout << ' ';
std::cout<<"Enter the element you want to search:";
std::cin>>ele;
bool found = (std::find(first.begin(), first.end(), ele) != first.end());
if(found)
std::cout<<"Element found";
else
std::cout<<"Element not found";
return 0;
}
Output :
Enter how many elements you want to insert in List1:2
Enter elements into List1:3
Enter elements into List1:2
Enter how many elements you want to insert in List2:2
Enter elements into List2:5
Enter elements into List2:6
List3: List1 concatenated to List2: 2 3 5 6
Enter the element you want to search:3
Element found
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.