Using inheritance, extend the template class indexList (posted on UR Courses) wi
ID: 3868485 • Letter: U
Question
Using inheritance, extend the template class indexList (posted on UR Courses) with the following functions . Concatenate two indexed lists. . Sort an indexed list using the selection sort algorithm (declared in indexList but not implemented) 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 resulting list and then display it. In addition, the driver will search for a target in the resulting list A sample run follows. List 1 of type "int" Enter number of list items: 4 Enter next item: 11 Enter next item: 7 Enter next item: 5 Enter next item: 34 List 2 of type "int'" Enter number of list items: 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 5Explanation / Answer
#include "indexList.h"
#include <iostream>
using namespace std;
template <class T, int maxSize>
class indexList
{
public:
// Constructor
indexList();
// Add an item to the end of an indexed list
bool append(const T&);
// Replace an element at a specified index
bool replace(int, const T&);
// Insert an element at a specified index
bool insert(int, const T&);
// Retrieve an element at a specified index
bool retrieve(int, T&) const;
//delete an element at an specified index
bool remove(int);
// Find index of smallest value in a sublist
int findMin(int, int) const;
// Find index of largest value in a sublist
int findMax(int, int) const;
// Find index of a target item
// Returns -1 if target item not found
int search(const T&) const;
// Sort an indexed list
void selSort();
// Read data into the list
void read(istream &);
// Display the list contents
void display() const;
// Get the current size
int getSize() const;
private:
//Data Members
T elements[maxSize];
int size;
};
template <class T, int maxSize>
indexList<T, maxSize>::indexList() {
size = 0;
}
// Add an item to the end of an indexed list
template <class T, int maxSize>
bool indexList<T, maxSize>::append(const T& item) {
if (size == maxSize)
return false;
elements[size++] = item;
return true;
}
// Replace an element at a specified index
template <class T, int maxSize>
bool indexList<T, maxSize>::replace(int index, const T& item) {
if (index < 0 || index >= size)
return false;
elements[index] = item;
return true;
}
// Insert an element at a specified index
template <class T, int maxSize>
bool indexList<T, maxSize>::insert(int index, const T& item) {
if (index < 0 || index > size)
return false;
for (int i = size; i > index; i--)
elements[i] = elements[i-1];
elements[index] = item;
size ++;
return true;
}
// Retrieve an element at a specified index
template <class T, int maxSize>
bool indexList<T, maxSize>::retrieve(int index, T& item) const {
if (index < 0 || index >= size)
return false;
item = elements[index];
return true;
}
//delete an element at an specified index
template <class T, int maxSize>
bool indexList<T, maxSize>::remove(int index) {
if (index < 0 || index >= size)
return false;
for (int i = index; i < size - 1; i++)
elements[i] = elements[i + 1];
size --;
return true;
}
// Find index of smallest value in a sublist
template <class T, int maxSize>
int indexList<T, maxSize>::findMin(int low, int high) const {
T min = low;
for (int i = low; i < high; i++)
if (elements[i] < elements[min])
min = i;
return min;
}
// Find index of largest value in a sublist
template <class T, int maxSize>
int indexList<T, maxSize>::findMax(int low, int high) const {
T max = low;
for (int i = low; i < high; i++)
if (elements[i] > elements[max])
max = i;
return max;
}
// Find index of a target item
// Returns -1 if target item not found
template <class T, int maxSize>
int indexList<T, maxSize>::search(const T& item) const {
int low = 0;
int high = size - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (elements[mid] == item)
return mid;
else if (elements[mid] < item)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
// Sort an indexed list
template <class T, int maxSize>
void indexList<T, maxSize>::selSort() {
for (int i = 0; i < size; i++) {
int min = i;
for (int j = i; j < size; j++)
if (elements[j] < elements[min])
min = j;
if (min != i) {
T tmp = elements[min];
elements[min] = elements[i];
elements[i] = tmp;
}
}
}
// Read data into the list
template <class T, int maxSize>
void indexList<T, maxSize>::read(istream &in) {
}
// Display the list contents
template <class T, int maxSize>
void indexList<T, maxSize>::display() const {
for (int i = 0; i < size; i++)
cout << elements[i] << endl;
}
// Get the current size
template <class T, int maxSize>
int indexList<T, maxSize>::getSize() const {
return size;
}
int main() {
}
Due to the length of the solution (14 methods) we are not able to provide the main method. Please co-operate.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.