Question: Use C++ template mechanism and define a generic SortedList ADT. Implement the SortedList ADT using a dynamically allocated array. Use the Student.cpp, SortedList.cpp, SortedList.h to

Use C++ template mechanism and define a generic SortedList ADT. Implement the SortedList ADT using a dynamically allocated array.

Use the Student.cpp, SortedList.cpp, SortedList.h to implement a tester.cpp (included below)

Implement tester.cpp. This program should test the SortedList ADT. The program will continue reading commands and data from the input file inFile until the command Quit is read. Commands are: (isEmpty, isFull, makeEmpty, getLength, get, insertItem, deleteItem, merge and printList) The program should write output to an output file outFile.txt.

Your test driver should define these lists:

SortedList integerList(5); // list of integers, default size.

SortedList charList; // a list of characters of size 20.

SortedList studentList(10); // Students list of size 10.

Your test driver should read list data type from the use in order to be able to test your implementation for the three types: int, char and Student (see the code below). make sure you are using the same commands and be aware of case sensitivity e.g. isEmpty not IsEmpty which will cause your program to fail the test.

SortedList.h

#include #include #include template class SortedList { public: SortedList(); bool isEmpty() const; bool isFull() const; int getLength() const; void insertItem(ItemType newItem); void deleteItem(ItemType item); ItemType get(int index); void makeEmpty(); void printList(); ~SortedList(); private: int length; int MAX_ITEMS; ItemType *info; int findIndex(ItemType item); };

SortedList.cpp

#include "SortedList.h" #include #include

template SortedList::SortedList() { MAX_ITEMS = 50; info = new ItemType[MAX_ITEMS]; length = 0; } template bool SortedList::isEmpty() const { return (length == 0); } template bool SortedList::isFull() const { return (length == MAX_ITEMS); } template int SortedList::getLength() const { return length; } template void SortedList::insertItem (ItemType newItem) { int location = 0; while(location < length) { if(newItem location; index --) info[index] = info [index - 1]; info[location] = newItem; length++; }

template void SortedList::deleteItem (ItemType item) { int location = 0; while (item != info[location]) location++; for (int index = location + 1; index

template void SortedList::makeEmpty () { delete []info; //deallocate previously allocated array //create a new empty list info = new ItemType[MAX_ITEMS]; length = 0; } template ItemType SortedList::get(int index) { if(index <0 || index>= length) throw std::out_of_range("Index out of range"); //throw out of range exception

return info[index]; }

template void SortedList::printList() { for(int i = 0; i < length; i++) std::cout << info[i] << std::endl; }

template SortedList::~SortedList() { delete []info; //deallocate the memory }

Student.cpp

#include "SortedList.cpp" using namespace std; class Student { private: int id; string name; public: Student() { id = 0; name = ""; } Student(int id_in, string name_in) { id = id_in; name = name_in; }

void setId(int id_in) { id = id_in; }

void setName(string name_in) { name = name_in; }

int getId() const { return id; }

string getName() const { return name; }

bool operator == (const Student &other) { return id == other.id; } bool operator < (const Student &other) { return id < other.id; }

bool operator > (const Student &other) { return id > other.id; }

friend ostream & operator << (ostream &out, const Student &s) { out << s.getId() << "\t" << s.getName(); return out; } };

--------------------------

tester.cpp

#include #include #include

#include  #include  #include "SortedList.h" 
using namespace std; 
void testIntegersList(); void testCharactersList(); void testStudentsList(); int main() 
{ int number; string command; 
 int datatype; 
// Prompt user to enter type of elements 

Cout<< Enter Elements Type 1 for integer 2 for character 3 for Student ; cin>> datatype; switch (datatype) { case 1: testIntegersList();

 break; case 2: testCharactersList(); 
 break; case 3: testStudentsList(); 

break;

} void testIntegersList() { ifstream inFile; ofstream outFile; inFile.open(intcommands.txt); outFile.open(outFile.txt); inFile>> command; // read commands from a text file SortedList list(30); while (command != Quit) { if (command== isEmpty())

 if( list.isEmpty()) outFile << list is empty; 
 else outFile << list is not empty; 
 else if (command == inserItem) { 
 inFile>> number; 
 list.inserItem(number); } 
.......... inFile>> command;} // while } // testIntegersList 
void testStudentsList() 

{ ifstream inFile; ofstream outFile; inFile.open(studcommands.txt); outFile.open(outFile.txt); inFile>> command; // read commands from a text file

SortedList list; Student StudentObj; while (command != Quit) { if (command== isEmpty()) 
 if( list.isEmpty()) outFile << list is empty; 
 else outFile << list is not empty; 
 else if (command ==  inserItem) { 
 inFile>> ID>>name; StudentObj.setID(ID) StudentObj.setName(name) list.inserItem(StudentObj); 

} ....

} // testStudentList 
void testcharactersList() { .......... } // testCharactersList 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!