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: 3848664 • Letter: U

Question

Use C++ template mechanism and define a generic SortedList ADT. Implement the SortedList ADT using a dynamically allocated array. Use SortedList.cpp and SortedList.h to implement a simple student class and tester.cpp

Implement a simple Student class with two data members: StudentID (int) and Student name (String). Implement a constructor and setter functions for class Student. You must also overload operators ==, < and > for class Student in order to be able to compare objects. For example, the operator == should return true if compared student objects have the same StudentID and false otherwise.

Code your SortedList driver program:

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.cpp

#include "SortedList.h"
#include <initializer_list>
#include <iostream>
#include <fstream>
using namespace std;

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>
ItemType SortedList<ItemType>::get(int index, ItemType item)
{
   index = 0;
   while(index < MAX_ITEMS)
   {
       if(item == info[index])
           return index;
       else
           return false;
   }
}


template<class ItemType>
void SortedList<ItemType>::makeEmpty()
{
   length = 0;
}

template<class ItemType>
void SortedList<ItemType>::printList()
{
   for(int i = 0; i < length; i++)
   {
       cout<< info <<"";
   }
}


template<class ItemType>
SortedList<ItemType>::~SortedList()
{
   delete []info;
}

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 prtinList();
~SortedList();

private:
int length;
int MAX_ITEMS;
ItemType *info;
int findIndex(ItemType item);
};

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

SortedList.cpp

#include "SortedList.h"
#include <initializer_list>
#include <iostream>
#include <fstream>
using namespace std;

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>
ItemType SortedList<ItemType>::get(int index, ItemType item)
{
   index = 0;
   while(index < MAX_ITEMS)
   {
       if(item == info[index])
           return index;
       else
           return false;
   }
}


template<class ItemType>
void SortedList<ItemType>::makeEmpty()
{
   length = 0;
}

template<class ItemType>
void SortedList<ItemType>::printList()
{
   for(int i = 0; i < length; i++)
   {
       cout<< info <<"";
   }
}


template<class ItemType>
SortedList<ItemType>::~SortedList()
{
   delete []info;
}

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 prtinList();
~SortedList();

private:
int length;
int MAX_ITEMS;
ItemType *info;
int findIndex(ItemType item);
};

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

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