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: 3848761 • 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)

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<int> integerList(5); // list of integers, default size.

SortedList<char> charList; // a list of characters of size 20.

SortedList<Student> 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 <cstdlib>
#include <iostream>
#include <initializer_list>
template<class ItemType>
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 <initializer_list>
#include <iostream>


template<class ItemType>
SortedList<ItemType>::SortedList()
{
MAX_ITEMS = 50;
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;
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;
while (item != info[location])
location++;
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()
{
for(int i = 0; i < length; i++)
std::cout << info[i] << std::endl;
}

template<class ItemType>
SortedList<ItemType>::~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 <iostream> #include <fstream>
#include <string>

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<int> 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

Answer:

Note: user given code is modified. Also the input files studcommands.txt, charcommands.txt, intcommands.txt are not given. So I am unable to run the code.

#include <cstdlib>

#include <iostream>

#include <initializer_list>

template<class ItemType>

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 <initializer_list>

#include <iostream>

template<class ItemType>

SortedList<ItemType>::SortedList()

{

MAX_ITEMS = 50;

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;

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;

while (item != info[location])

location++;

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

{

for(int i = 0; i < length; i++)

std::cout << info[i] << std::endl;

}

template<class ItemType>

SortedList<ItemType>::~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;

}

};

File Name: tester.cpp

#include <iostream>

#include <fstream>

#include <string>

#include <cctype>

#include <cstring>

#include "SortedList.h"

using namespace std;

void testIntegersList();

void testCharactersList();

void testStudentsList();

//main

int main()

{

//Declare varible

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 case

switch (datatype)

{

case 1: testIntegersList();

break;

case 2: testCharactersList();

break;

case 3: testStudentsList();

break;

}

return 0;

}

//Define method

void testIntegersList()

{

ifstream inFile;

ofstream outFile;

int number;

string command;

SortedList<int> list(5);

inFile.open("intcommands.txt");

outFile.open("outFile1.txt");

inFile>> command; // read commands from a text file

//Infinite Loop

while (command != “Quit”)

{

//If isEmpty

if (command== "isEmpty")

if( list.isEmpty())

outFile << “list is empty”<<endl;

else

outFile << “list is not empty"<<endl;

//If isFull

if (command== "isFull")

if( list.isFull())

outFile << “list is Full”<<endl;

else

outFile << “list is not Full”<<endl;

//If makeEmpty

if (command== "makeEmpty")

{   

list.makeEmpty();

outFile << “list is emptied."<<endl;

}

//If getLength

if (command== "getLength")

{

outFile << “list Length:"<<list.getLength()<<endl;

}

//If get

if (command== "get")

{   

inFile>>number;

outFile << “Item at Index:"<<list.get(number)<<endl;

}

//If command is insertitem

else if (command == “insertItem”)

{

inFile>> number;

list.insertItem(number);

}

//If command is deleteitem

else if (command == “deleteItem”)

{

inFile>> number;

list.deleteItem(number);

}

//If command is printList

else if (command == “printList”)

{

list.printList();

}

//If command is merge

else if(command == "merge")

{

}

//Read next command

inFile>> command;

} // while

//Close files

inFile.close();

outFile.close();

} // testIntegersList

//Define method

void testStudentsList()

{

//Declare variables

int ID;

int number;

string name;

string command;

//File pointers

ifstream inFile;

ofstream outFile;

//Open the files

inFile.open("studcommands.txt");

outFile.open("outFile2.txt");

inFile>> command; // read commands from a text file

//Create list

SortedList<Student> list(10);

Student StudentObj;

//Infinite loop

while (command != “Quit”)

//If command is isEmpty

{ if (command== "isEmpty")

if( list.isEmpty())

outFile << “list is empty”<<endl;

else

outFile << “list is not empty"<<endl;

//If command isFull

if (command== "isFull")

if( list.isFull())

outFile << “list is Full”<<endl;

else

outFile << “list is not Full”<<endl;

//If command is makeEmpty

if (command== "makeEmpty")

{   

list.makeEmpty();

outFile << “list is emptied."<<endl;

}

//If command is getLength

if (command== "getLength")

{

outFile << “list Length:"<<list.getLength()<<endl;

}

//If command is get

if (command== "get")

{   

inFile>>number;

outFile << “Item at Index:"<<list.get(number)<<endl;

}

//If command is insertItem

else if (command == “insertItem”)

{

inFile>> ID>>name;

StudentObj.setID(ID)

StudentObj.setName(name)

list.inserItem(StudentObj);

}

//If command is deleteItem

else if (command == “deleteItem”)

{

inFile>> ID;

list.deleteItem(ID);

}

//If command is printList

else if (command == “printList”)

{

list.printList();

}

//If command is merge

else if(command == "merge")

{

}

//Read next command

inFile>> command;

}

//Close files

inFile.close();

outFile.close();

} // testStudentList

//Define method

void testcharactersList()

{

//Declare variable

char c;

int number;

string command;

//File pointers

ifstream inFile;

ofstream outFile;

//Open files

inFile.open("charcommands.txt");

outFile.open("outFile3.txt");

inFile>> command; // read commands from a text file

SortedList<char> charList;

//Infinite loop

while (command != “Quit”)

{

//If command is isEmpty

if (command== "isEmpty")

if( charList.isEmpty())

outFile << “list is empty”<<endl;

else

outFile << “list is not empty"<<endl;

//If command is isFull

if (command== "isFull")

if( charList.isFull())

outFile << “list is Full”<<endl;

else

outFile << “list is not Full”<<endl;

//If command is makeEmpty

if (command== "makeEmpty")

{   

charList.makeEmpty();

outFile << “list is emptied."<<endl;

}

//If Command is getLength

if (command== "getLength")

{   

outFile << “list Length:"<<charList.getLength()<<endl;

}

//If command is get

if (command== "get")

{   

inFile>>number;

outFile << “Item at Index:"<<charList.get(number)<<endl;

}

//If command is insertItem

else if (command == “insertItem”)

{

inFile>> c;

charList.inserItem(c);

}

//If command is deleteItem

else if (command == “deleteItem”)

{

inFile>> c;

charList.deleteItem(c);

}

//If command is printList

else if (command == “printList”)

{

charList.printList();

}

//If command is merge

else if(command == "merge")

{

}

//Read next command

inFile>> command;

}

//Close files

inFile.close();

outFile.close();

} // testCharactersList

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