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

Question

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

Add the following methods to SortedList.cpp and Implement a simple Student class

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.

Methods to be added to SortedList.cpp

ItemType get(int index);

Function: Returns the element at the specified position (index) in this list. Precondition: List is initialized.
Postcondition: The function returns the element at the specified position in this list.

or throws IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= length of the list).

void makeEmpty();

Function: Reinitializes the list to empty state. Deallocates all dynamically allocated data members. Precondition: None.
Postcondition: List is empty.

void printList();

Function: Prints list elements.
Precondition: list is initialized.
Postcondition: List elements are displayed on the screen.

~SortedList(); // class destructor
Function: Reinitializes the list to empty state. Deallocates all dynamically allocated data members. Precondition: None.
Postcondition: List is empty.

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

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);
};

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--;
}

Explanation / Answer

Here is the code for the question. SortedList.h and SortedList.cpp are modified. Student.cpp is a new file.

Please don't forget to rate the answer if it helped. Thank you very much.

compile : g++ SortedList.cpp Student.cpp

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;
}
};

int main()
{
string ans;
string name;
int id;
SortedList<Student> students;

while(true)
{
cout <<"Add a student ? y/n : ";
cin >> ans;
if(ans != "y" && ans != "Y")
break;

Student s;

cout << "Enter Id: ";
cin >> id;
s.setId(id);

cout << "Enter name: ";
cin >> name;
s.setName(name);

students.insertItem(s);
}

cout << "The sorted list of students is " << endl;
students.printList();
}

output

Add a student ? y/n : y
Enter Id: 5
Enter name: John
Add a student ? y/n : y
Enter Id: 1
Enter name: Peter
Add a student ? y/n : y
Enter Id: 9
Enter name: Bob
Add a student ? y/n : n
The sorted list of students is
1   Peter
5   John
9   Bob

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