In: Computer Science
C++ Problem. I am providing the code. Just Please provide the new function and highlight it.
implement the functions replaceAt, seqSearch, and remove.
Test your new function too in main. Also, Test Old functions in main. Show the output.
Also, modify the functions accordingly which have "See Programming Exercise 22".
main.cpp :
#include <iostream>
using namespace std;
#include "arrayListTypetempl.h"
int main(){
arrayListType<int> intList;
arrayListType<char> charList;
intList.insertEnd(5);
intList.insertEnd(3);
intList.insertEnd(4);
intList.insertEnd(55);
charList.insertEnd('a');
charList.insertEnd('B');
charList.insertEnd('f');
intList.print();
charList.print();
return 0;
}
arrayListTypetempl.h :
#ifndef H_arrayListType
#define H_arrayListType
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>&
operator=(const arrayListType<elemType>&);
//Overloads the assignment operator
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem)
const;
void clearList();
void replaceAt(int location, const elemType& repItem);
int seqSearch(const elemType& searchItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType (const arrayListType<elemType>&
otherList);
//Copy constructor
~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.
protected:
elemType *list; //array to hold the list elements
int length; //variable to store the length of the list
int maxSize; //variable to store the maximum
//size of the list
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
if (length == 0){
return true;
}
else {
return false;
}
} // //end isEmpty
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
cout << "See Programming Exercise 22." << endl;
return false;
} //end isFull
template <class elemType>
int arrayListType<elemType>::listSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //end listSize
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
cout << "See Programming Exercise 22." << endl;
return -1;
} //end maxListSize
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
} //end print
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int
location,
const elemType& item) const
{
if (location < 0 || location >= length)
{
cout << "The location of the item to be removed "
<< "is out of range." << endl;
return false;
}
else
return (list[location] == item);
} //end isItemAtEqual
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "The location of the item to be removed "
<< "is out of range." << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location,
elemType& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
cout << "See Programming Exercise 22." << endl;
} //end clearList
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of the size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
} //end constructor
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
} //end destructor
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
cout << "See Programming Exercise 22." << endl;
}//end copy constructor
template <class elemType>
const arrayListType<elemType>&
arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
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;
} //end overloading operatror=
template <class elemType>
void arrayListType<elemType>::insertAt(int location, const
elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType&
insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
#endif
The required code has been provided below followed by a sample output. Also, the remove function is implemented such that if the element is present in the list more than 1 time then it removes the element from 1st occurrence index and if not found then does nothing.
CODE:
#include <iostream>
using namespace std;
#include "arrayListTypetempl.h"
int main(){
arrayListType<int> intList;
arrayListType<char> charList;
intList.insertEnd(5);
intList.insertEnd(3);
intList.insertEnd(4);
intList.insertEnd(55);
charList.insertEnd('a');
charList.insertEnd('B');
charList.insertEnd('f');
intList.print();
charList.print();
// checking all new functons implemented
intList.replaceAt(1,23);
charList.replaceAt(2,'c');
cout<<"After intList.replaceAt(1,23) and charList.replaceAt(2,'c') the lists are:"<<endl;
intList.print();
charList.print();
int res = intList.seqSearch(55);
if (res>=0)
cout<<"55 found at index "<<res<<endl;
else
cout<<"55 not found "<<endl;
res = charList.seqSearch('d');
if (res>=0)
cout<<"char d found at index "<<res<<endl;
else
cout<<"char d not found "<<endl;
intList.remove(4);
charList.remove('a');
cout<<"After intList.remove(4) and charList.remove('a') the lists are:"<<endl;
intList.print();
charList.print();
return 0;
}
//arrayListTypetempl.h :
// IMPLEMENTED NEW FUNCTIONS
#ifndef H_arrayListType
#define H_arrayListType
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>&
operator=(const arrayListType<elemType>&);
//Overloads the assignment operator
bool isEmpty() const;
bool isFull() const;
int listSize() const;
int maxListSize() const;
void print() const;
bool isItemAtEqual(int location, const elemType& item) const;
void insertAt(int location, const elemType& insertItem);
void insertEnd(const elemType& insertItem);
void removeAt(int location);
void retrieveAt(int location, elemType& retItem) const;
void clearList();
void replaceAt(int location, const elemType& repItem);
int seqSearch(const elemType& searchItem);
void remove(const elemType& removeItem);
arrayListType(int size = 100);
arrayListType (const arrayListType<elemType>& otherList);
//Copy constructor
~arrayListType();
//Destructor
//Deallocate the memory occupied by the array.
protected:
elemType *list; //array to hold the list elements
int length; //variable to store the length of the list
int maxSize; //variable to store the maximum
//size of the list
};
template <class elemType>
bool arrayListType<elemType>::isEmpty() const
{
if (length == 0){
return true;
}
else {
return false;
}
} // //end isEmpty
template <class elemType>
bool arrayListType<elemType>::isFull() const
{
if (length==maxSize)
return true;
return false;
} //end isFull
template <class elemType>
int arrayListType<elemType>::listSize() const
{
return length;
} //end listSize
template <class elemType>
int arrayListType<elemType>::maxListSize() const
{
return maxSize;
} //end maxListSize
template <class elemType>
void arrayListType<elemType>::print() const
{
for (int i = 0; i < length; i++)
cout << list[i] << " ";
cout << endl;
} //end print
template <class elemType>
bool arrayListType<elemType>::isItemAtEqual(int location,
const elemType& item) const
{
if (location < 0 || location >= length)
{
cout << "The location of the item to be removed "
<< "is out of range." << endl;
return false;
}
else
return (list[location] == item);
} //end isItemAtEqual
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
if (location < 0 || location >= length)
cout << "The location of the item to be removed "
<< "is out of range." << endl;
else
{
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
} //end removeAt
template <class elemType>
void arrayListType<elemType>::retrieveAt(int location,
elemType& retItem) const
{
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "
<< "out of range" << endl;
else
retItem = list[location];
} //end retrieveAt
template <class elemType>
void arrayListType<elemType>::clearList()
{
length = 0;
} //end clearList
template <class elemType>
void arrayListType<elemType>::replaceAt(int location, const elemType& repItem){
if (location < 0 || location >= length)
cout << "The location of the item to be retrieved is "<< "out of range" << endl;
else
list[location] = repItem;
}
template <class elemType>
int arrayListType<elemType>::seqSearch(const elemType& searchItem){
for(int location=0;location<length;location++)
{
if (list[location]==searchItem)
return location;
}
return -1;
}
template <class elemType>
void arrayListType<elemType>::remove(const elemType& removeItem){
int location = -1;
for(int i=0;i<length;i++)
{
if (list[i]==removeItem){
location = i;
break;
}
}
if (location>=0){
for (int i = location; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
}
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size <= 0)
{
cout << "The array size must be positive. Creating "
<< "an array of the size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
} //end constructor
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
} //end destructor
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
length = otherList.length;
maxSize = otherList.maxSize;
for(int i=0;i<length;i++)
list[i] = otherList[i];
}//end copy constructor
template <class elemType>
const arrayListType<elemType>& arrayListType<elemType>::operator=
(const arrayListType<elemType>& otherList)
{
if (this != &otherList) //avoid self-assignment
{
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;
} //end overloading operatror=
template <class elemType>
void arrayListType<elemType>::insertAt(int location, const elemType& insertItem)
{
if (location < 0 || location >= maxSize)
cout << "The position of the item to be inserted "
<< "is out of range." << endl;
else if (length >= maxSize) //list is full
cout << "Cannot insert in a full list" << endl;
else
{
for (int i = length; i > location; i--)
list[i] = list[i - 1]; //move the elements down
list[location] = insertItem; //insert the item at
//the specified position
length++; //increment the length
}
} //end insertAt
template <class elemType>
void arrayListType<elemType>::insertEnd(const elemType& insertItem)
{
if (length >= maxSize) //the list is full
cout << "Cannot insert in a full list." << endl;
else
{
list[length] = insertItem; //insert the item at the end
length++; //increment the length
}
} //end insertEnd
#endif
OUTPUT:
NOTE: In case of any query, you can mention it in comment section. HAPPY LEARNING!!