Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use C++ template mechanism and define a generic SortedList ADT. Implement the So

ID: 3848917 • Letter: U

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 implement a tester.cpp (included below) and text files

you are allow to implement Student.cpp SortedList.cpp SortedList.h

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.

Textfiles:

Test your program with 3 text file intcommands.txt, chcommands.txt and studcommands.txt each contains testing commands for specific data type your test driver should carefully associate the inFile with the corresponding commands file. Our command file for integer lists is called int intcommands.txt. Our commands file to test a list of characters is called chcommands.txt and our text file to test a list of student objects is called studcommands.txt. Make sure that the program will give the correct results and will not stop due to any run-time error

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 break;
else
location++;
}
for (int index = length; index > 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 info [index-1] = info[index];
length--;
}

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() << " " << s.getName();
return out;
}
};

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

tester.cpp

#include #include
#include

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

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())

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

} ....

Explanation / Answer

Here is the completed code. I found few bugs in SortedList insert and delete functions . Have fixed them. Sample input and output files are attached. Please don't forget to rate the answer if it helped. Thank you very much

SortedList.h

#include <iostream>

template <class ItemType>
class SortedList
{
public:
SortedList();
SortedList(int max);
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(std::ostream &out);
~SortedList();
private:
int length;
int MAX_ITEMS;
ItemType *info;
int findIndex(ItemType item);
};

SortedList.cpp

#include "SortedList.h"
#include <iostream>


template <class ItemType>
SortedList<ItemType>::SortedList()
{
MAX_ITEMS = 50;
info = new ItemType[MAX_ITEMS];
length = 0;
}
template <class ItemType>
SortedList<ItemType>::SortedList(int max)
{
MAX_ITEMS = max;
info = new ItemType[MAX_ITEMS];
length = 0;
}
template <class ItemType>
bool SortedList<ItemType>::isEmpty() const
{
return (length == 0);
}
template <class ItemType>
bool SortedList<ItemType>::isFull() const
{
return (length == MAX_ITEMS);
}
template <class ItemType>
int SortedList<ItemType>::getLength() const
{
return length;
}
template <class ItemType>
void SortedList<ItemType>::insertItem (ItemType newItem)
{
int location = 0;
if(length == MAX_ITEMS)
return;
while(location < length)
{
if(newItem<info[location])
break;
else
location++;
}
for (int index = length; index > location; index --)
info[index] = info [index - 1];
info[location] = newItem;
length++;
}
template <class ItemType>
void SortedList<ItemType>::deleteItem (ItemType item)
{
int location = 0;
int found = 0;
while (location < length )
{
if(item == info[location])
{
found = 1;
break;
}
location++;
}
if(found)
{
for (int index = location + 1; index < length ; index++)
info[index - 1] = info[index];
length--;
}
}
template <class ItemType>
void SortedList<ItemType>::makeEmpty ()
{
delete []info; //deallocate previously allocated array
//create a new empty list
info = new ItemType[MAX_ITEMS];
length = 0;
}
template <class ItemType>
ItemType SortedList<ItemType>::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 <class ItemType>
void SortedList<ItemType>::printList(std::ostream &out)
{
for(int i = 0; i < length; i++)
out << info[i] << std::endl;
}
template <class ItemType>
SortedList<ItemType>::~SortedList()
{
delete []info; //deallocate the memory
}

Student.h

#include <iostream>
using namespace std;
class Student
{
private:
int id;
string name;
public:
//default constructor
Student()
{
id = 0;
name = "";
}
//constructor with parameters
Student(int id1, string name1)
{
id = id1;
name = name1;
}
//setter and getters
void setId(int id1)
{
id = id1;
}

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

int getId() const
{
return id;
}

string getName() const
{
return name;
}

//operator to compare students on id, true if both ids are same
bool operator == (const Student &s2)
{
return id == s2.id;
}
//operator < to check if the id of this object is less than s2.id
bool operator < (const Student &s2)
{
return id < s2.id;
}

//operator > to check if the id of this object is greater than s2.id
bool operator > (const Student &s2)
{
return id > s2.id;
}

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

tester.cpp

#include "SortedList.cpp" //for template in .h to work otherwise the functions are not linked
#include "Student.h"
#include <fstream>
#include <iostream>
using namespace std;
void testIntegersList();
void testCharactersList();
void testStudentsList();
int main()
{ int number;
string command;
int datatype;
SortedList<int> integerList(5);

// 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;
int number;
string command;
SortedList<int> list(5);
inFile.open("intcommands.txt");
outFile.open("intOutFile.txt");
if(inFile.fail() || outFile.fail())
{
cout << "Error opening files" << endl;
return;
}
while (inFile >> command)
{
if (command== "isEmpty")
{
if( list.isEmpty())
outFile << "list is empty" << endl;
else
outFile << "list is not empty" <<endl;
}
else if (command == "isFull")
{
if( list.isFull())
outFile << "list is full" << endl;
else
outFile << "list is not full" << endl;
}

else if (command == "makeEmpty")
{
list.makeEmpty();
outFile << "list is emptied" << endl;
}
else if (command == "getLength")
{
outFile << "length of list is " << list.getLength() << endl;
}
else if(command == "get")
{
int index;
inFile >> index;
outFile << "number at index " << index << " is " << list.get(index) << endl;
}
else if(command == "printList")
{
outFile << "printing list contents" << endl;
list.printList(outFile);
}
else if (command == "insertItem")
{
inFile >> number;
list.insertItem(number);

}
else if (command == "deleteItem")
{
inFile >> number;
list.deleteItem(number);
}
}
inFile.close();
outFile.close();
cout << "output written to intOutFile.txt" <<endl;
} // testIntegersList

void testStudentsList()
{


SortedList<Student> list;
Student StudentObj;
int id;
string name;
ifstream inFile;
ofstream outFile;
string command;
inFile.open("studcommands.txt");
outFile.open("studOutFile.txt");

if(inFile.fail() || outFile.fail())
{
cout << "Error opening files" << endl;
return;
}
while (inFile >> command)
{
if (command== "isEmpty")
{
if( list.isEmpty())
outFile << "list is empty" << endl;
else
outFile << "list is not empty" <<endl;
}
else if (command == "isFull")
{
if( list.isFull())
outFile << "list is full" << endl;
else
outFile << "list is not full" << endl;
}

else if (command == "makeEmpty")
{
list.makeEmpty();
outFile << "list is emptied" << endl;
}
else if (command == "getLength")
{
outFile << "length of list is " << list.getLength() << endl;
}
else if(command == "get")
{
int index;
inFile >> index;
StudentObj = list.get(index);
outFile << "student at index " << index << " is " << StudentObj.getId() << ", " << StudentObj.getName() << endl;
}
else if(command == "printList")
{
outFile << "printing list contents" << endl;
list.printList(outFile);
}
else if (command == "insertItem")
{
inFile >> id >> name;
StudentObj.setId(id);
StudentObj.setName(name);
list.insertItem(StudentObj);

}
else if (command == "deleteItem")
{
inFile >> id ;
StudentObj.setId(id);
list.deleteItem(StudentObj);
}
}
inFile.close();
outFile.close();
cout << "output written to studOutFile.txt" << endl;
} // testStudentList

void testCharactersList( )
{
ifstream inFile;
ofstream outFile;
char ch;
string command;
SortedList<char> list(20);
inFile.open("chcommands.txt");
outFile.open("chOutFile.txt");
if(inFile.fail() || outFile.fail())
{
cout << "Error opening files" << endl;
return;
}
while (inFile >> command)
{
if (command== "isEmpty")
{
if( list.isEmpty())
outFile << "list is empty" << endl;
else
outFile << "list is not empty" <<endl;
}
else if (command == "isFull")
{
if( list.isFull())
outFile << "list is full" << endl;
else
outFile << "list is not full" << endl;
}

else if (command == "makeEmpty")
{
list.makeEmpty();
outFile << "list is emptied" << endl;
}
else if (command == "getLength")
{
outFile << "length of list is " << list.getLength() << endl;
}
else if(command == "get")
{
int index;
inFile >> index;
outFile << "char at index " << index << " is " << list.get(index) << endl;
}
else if(command == "printList")
{
outFile << "printing list contents" << endl;
list.printList(outFile);
}
else if (command == "insertItem")
{
inFile >> ch;
list.insertItem(ch);

}
else if (command == "deleteItem")
{
inFile >> ch;
list.deleteItem(ch);
}
}
inFile.close();
outFile.close();
cout << "output written to chOutFile.txt" <<endl;
} // testcharactersList

input file intcomands.txt

isEmpty
isFull
insertItem 5
insertItem 3
insertItem 10
printList
deleteItem 8
deleteItem 3
printList
isEmpty
isFull
insertItem 20
insertItem 30
insertItem 40
isFull
printList
quit

input file chcommands.txt

insertItem h
insertItem e
insertItem l
insertItem l
insertItem o
printList
getLength
get 3
isEmpty
isFull
deleteItem l
printList
makeEmpty
printList
quit

input file studcommands.txt

insertItem 5 John
insertItem 2 Bob
printList
get 0
getLength
isEmpty
isFull
insertItem 1 Alice
printList
deleteItem 2
printList
quit

output file intOutFile.txt

list is empty
list is not full
printing list contents
3
5
10
printing list contents
5
10
list is not empty
list is not full
list is full
printing list contents
5
10
20
30
40

output file chOutFile.txt

printing list contents
e
h
l
l
o
length of list is 5
char at index 3 is l
list is not empty
list is not full
printing list contents
e
h
l
o
list is emptied
printing list contents

output file studOutFile.txt

printing list contents
2   Bob
5   John
student at index 0 is 2, Bob
length of list is 2
list is not empty
list is not full
printing list contents
1   Alice
2   Bob
5   John
printing list contents
1   Alice
5   John

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote