This is C++. PLease sumbit all files of code with file names Many of the functio
ID: 3728275 • Letter: T
Question
This is C++. PLease sumbit all files of code with file names
Many of the functions implemented in the abstract base class arrayListType apply to all lists, whether ordered or unordered. However, the pure virtual fnctions in the abstract base class will be implemented differently depending on whether or not the list is ordered (sorted). Here is the class invariant for the orderedArrayList type with the differences highlighted:
Items must be stored in ascending order
The length private instance variable keeps track of the number of items currently in the list.
New items are added so that the ascending order between items is maintained.
No gaps are allowed between items.
It doesn't matter what values are stored in locations beyond length.
The number of items in the array cannot exceed the value stored in the maxSize private instance variable.
Notice that some of the pure virtual functions really only apply to an unordered list. The problem is that they must still be implemented in the orderedArrayList class. Write the definition of the functions of the orderedArrayListType that are not provided in the source files found in the Chapter 12/Source Code from Textbook/arrayList ADT folder for the orderedArrayListType. The insert function that stores an item in the proper location is provided. You must implement the other functions. Note that seqSearch can be modified to be more efficient because the list is ordered.
Turn in all your header and implementation files and your test program. Also turn in one or more screen shots showing the results of your testing.
Explanation / Answer
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
using namespace std;
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=(const arrayListType<elemType>&);//
bool isEmpty() const;//
bool isFull() const;//
int listSize() const;//
int maxListSize() const;//
void print() const;//
bool isItemAtEqual(int,const elemType&) const;//
virtual void insertAt(int location,const elemType& insertItem) = 0;//
virtual void insertEnd(const elemType& insertItem) = 0;//
void removeAt(int);//
void retrieveAt(int,elemType&) const;//
virtual void replaceAt(int location, const elemType& repItem) = 0;//
void clearList();//
virtual int seqSearch(const elemType& searchItem) = 0;//
virtual void remove(const elemType& removeItem) = 0;//
arrayListType(int size = 100);//
arrayListType(const arrayListType<elemType>& otherList);//
virtual ~arrayListType();//
protected:
elemType *list;
int length;
int maxSize;
};
template <class elemType>
void arrayListType<elemType>::print() const
{
for(int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
}
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int location, const elemType& item) const
{
if(location < 0 || location >= length)
{
cout << "Location is out of range. " << endl;
return false;
}
else
return (list[location] == item);
}
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if(location < 0 || location >= length)
cout << "Location is out of range." << endl;
else
{
for(int i = location; i < length; i++)
list[i] = list[i + 1];
length--;
}
}
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location, elemType& retItem) const
{
if(location < 0 || location >= length)
cout << "Location is oit of range." << endl;
else
retItem = list[location];
}
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if(size <= 0)
{
cout << "Size must be positive, creating default size of 100." << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator =(const arrayListType<elemType>& otherList)
{
if(this != &otherList)
{
delete [] list;
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
for(int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
return (length == 0);
}
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
return (length == maxSize);
}
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
}
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
}
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
}
template <class elemType>
arrayListType<elemType>::arrayListType(const arrayListType<elemType> &otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize];
for(int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
#endif
unorderedArrayListType
#ifndef H_unorderedArrayListType
#define H_unorderedArrayListType
#include "arrayListType.h"
#include <iostream>
using namespace std;
template <class elemType>
class unorderedArrayListType: public arrayListType<elemType>
{
public:
void insertAt(int location,const elemType& insertItem);//
void insertEnd(const elemType& insertItem);//
void replaceAt(int location,const elemType& repItem);//
int seqSearch(const elemType& searchItem) const;//
void remove(const elemType& removeItem);//
unorderedArrayListType(int size = 100);//
};
template <class elemType>
void unorderedArrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if(length >= maxSize)
cout << "The list is full. " << endl;
else
{
list[length] = insertItem;
length++;
}
}
template <class elemType>
int unorderedArrayListType<elemType>::seqSearch(const elemType& searchItem) const
{
int loc;
bool found = false;
for(loc = 0; loc < length; loc++)
if(list[loc] == searchItem)
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
}
template <class elemType>
void unorderedArrayListType<elemType>::remove(const elemType& removeItem)
{
int loc;
if(length == 0)
cout << "The list is empty. " << endl;
else
{
loc = seqSearch(removeItem);
if(loc != -1)
removeAt(loc);
else
cout << "The item is not in the list. " << endl;
}
}
template <class elemType>
void unorderedArrayListType<elemType>::replaceAt(int location, const elemType& repItem)
{
if(location < 0 || location >= length)
cout << "Location is out of range. " << endl;
else
list[location] = repItem;
}
template <class elemType>
void unorderedArrayListType<elemType>::insertAt(int location, const elemType& insertItem)
{
if(location < 0 || location >= maxSize)
cout << "Location is out of range." << endl;
else if (length >= maxSize)
cout << "The list is full. " << endl;
else
{
for(int i = length; i > location; i--)
list[i] = list[i - 1];
list[location] = insertItem;
length++;
}
}
template <class elemType>
unorderedArrayListType<elemType>::unorderedArrayListType(int size)
:arrayListType<elemType>(size)
{
}
#endif
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.