In: Computer Science
Your primary task for this exercise is to complete header file by writing three functions with its description below:
removeAt function – to remove the item from the list at the position specified by location. Because the list elements are in no particular order (unsorted list), you could simple remove the element by swapping the last element of the list with the item to be removed and reducing the length of the list.
insertAt function - to insert an item in the list at the position specified by location. The item to be inserted is passed as a parameter to the function.
print function – to output the elements of the list.
3. Demonstrate the program by asking a user to enter 5 integers. After displaying 5 integers, ask the user the position of the item to be deleted. You can use your own data to generate two outputs.
Sample output 1:
Enter 5 integers: 45 19 2 16 77
The list you entered is: 45 19 2 16 77
Enter the position of item to be deleted: 2
After removing element at 2, the list is:
45 19 77 16
Sample output 2:
Enter 5 integers: 15 12 11 3 49
The list you entered is: 15 12 11 3 49
Enter the position of item to be deleted: 6
The location of the item to be removed is out of range
After removing element at 6, the list is:
15 12 11 3 49
======================================== C++
#ifndef H_arrayListType #define H_arrayListType #include <iostream> #include <cassert> using namespace std; template <class elemType> class arrayListType { public: const arrayListType<elemType>& operator= (const arrayListType<elemType>&); //Overloads the assignment operator void print() const; //Function to output the elements of the list //Postcondition: Elements of the list are output on the // standard output device. void insertAt(int location, const elemType& insertItem); //Function to insert an item in the list at the //position specified by location. The item to be inserted //is passed as a parameter to the function. //Postcondition: Starting at location, the elements of the // list are shifted down, list[location] = insertItem;, // and length++;. If the list is full or location is // out of range, an appropriate message is displayed. void removeAt(int location); //Function to remove the item from the list at the //position specified by location //Postcondition: The list element at list[location] is removed // and length is decremented by 1. If location is out of // range,an appropriate message is displayed. arrayListType(int size = 100); //constructor //Creates an array of the size specified by the //parameter size. The default array size is 100. //Postcondition: The list points to the array, length = 0, // and maxSize = size arrayListType(const arrayListType<elemType>& otherList); //copy constructor ~arrayListType(); //destructor protected: elemType *list; //array to hold the list elements int length; //to store the length of the list int maxSize; //to store the maximum size of the list }; // print function definition template <class elemType> void arrayListType<elemType>::print() const { // your code here } // insertAt function definition template <class elemType> void arrayListType<elemType>::insertAt (int location, const elemType& insertItem) { // your code here } //end insertAt // removeAt function definition template <class elemType> void arrayListType<elemType>::removeAt(int location) { // your code here } //end removeAt // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ template <class elemType> arrayListType<elemType>::arrayListType(int size) { if (size < 0) { cerr << "The array size must be positive. Creating " << "an array of size 100. " << endl; maxSize = 100; } else maxSize = size; length = 0; list = new elemType[maxSize]; assert(list != NULL); } template <class elemType> arrayListType<elemType>::~arrayListType() { delete [] list; } template <class elemType> arrayListType<elemType>::arrayListType (const arrayListType<elemType>& otherList) { maxSize = otherList.maxSize; length = otherList.length; list = new elemType[maxSize]; //create the array assert(list != NULL); //terminate if unable to allocate //memory space for (int j = 0; j < length; j++) //copy otherList list [j] = otherList.list[j]; } //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]; //create the array assert(list != NULL); //if unable to allocate memory //space, terminate the program for (int i = 0; i < length; i++) list[i] = otherList.list[i]; } return *this; } #endif
==================================================================
#include <iostream> #include "arrayListType.h" using namespace std; int main() { arrayListType<int> intList(100); int counter; int number; int position; cout << "Enter 5 integers: "; for (counter = 0; counter < 5; counter++) { cin >> number; intList.insertAt(counter, number); } cout << endl; cout << "The list you entered is: "; intList.print(); cout << endl; cout << "Enter the position of item to be deleted: "; cin >> position; intList.removeAt(position); cout << "After removing element at " << position << ", the list is:" << endl; intList.print(); system("pause"); return 0; } /* Enter 5 integers: 45 19 2 16 77 The list you entered is: 45 19 2 16 77 Enter the position of item to be deleted: 2 After removing element at 2, the list is: 45 19 77 16 */ /* Enter 5 integers: 15 12 11 3 49 The list you entered is: 15 12 11 3 49 Enter the position of item to be deleted: 6 The location of the item to be removed is out of range After removing element at 6, the list is: 15 12 11 3 49 */
#ifndef H_arrayListType
#define H_arrayListType
#include <iostream>
#include <cassert>
using namespace std;
template <class elemType>
class arrayListType
{
public:
const arrayListType<elemType>& operator=
(const arrayListType<elemType>&);
//Overloads the assignment operator
void print() const;
//Function to output the elements of the list
//Postcondition: Elements of the list are output on the
// standard output device.
void insertAt(int location, const elemType& insertItem);
//Function to insert an item in the list at the
//position specified by location. The item to be inserted
//is passed as a parameter to the function.
//Postcondition: Starting at location, the elements of the
// list are shifted down, list[location] = insertItem;,
// and length++;. If the list is full or location is
// out of range, an appropriate message is displayed.
void removeAt(int location);
//Function to remove the item from the list at the
//position specified by location
//Postcondition: The list element at list[location] is removed
// and length is decremented by 1. If location is out of
// range,an appropriate message is displayed.
arrayListType(int size = 100);
//constructor
//Creates an array of the size specified by the
//parameter size. The default array size is 100.
//Postcondition: The list points to the array, length = 0,
// and maxSize = size
arrayListType(const arrayListType<elemType>& otherList);
//copy constructor
~arrayListType();
//destructor
protected:
elemType *list; //array to hold the list elements
int length; //to store the length of the list
int maxSize; //to store the maximum size of the list
};
// print function definition
template <class elemType>
void arrayListType<elemType>::print() const
{
// your code here
for (int i = 0; i < length; i++)
cout << list[i] <<" ";
}
// insertAt function definition
template <class elemType>
void arrayListType<elemType>::insertAt
(int location, const elemType& insertItem)
{
// your code here
list[length++] = insertItem;
} //end insertAt
// removeAt function definition
template <class elemType>
void arrayListType<elemType>::removeAt(int location)
{
// your code here
if (location >= length || length == 0)
cout <<"The location of the item to be removed is out of range"<<endl;
else {
list[location] = list[length-1];
length--;
}
} //end removeAt
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
template <class elemType>
arrayListType<elemType>::arrayListType(int size)
{
if (size < 0)
{
cerr << "The array size must be positive. Creating "
<< "an array of size 100. " << endl;
maxSize = 100;
}
else
maxSize = size;
length = 0;
list = new elemType[maxSize];
assert(list != NULL);
}
template <class elemType>
arrayListType<elemType>::~arrayListType()
{
delete [] list;
}
template <class elemType>
arrayListType<elemType>::arrayListType
(const arrayListType<elemType>& otherList)
{
maxSize = otherList.maxSize;
length = otherList.length;
list = new elemType[maxSize]; //create the array
assert(list != NULL); //terminate if unable to allocate
//memory space
for (int j = 0; j < length; j++) //copy otherList
list [j] = otherList.list[j];
} //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]; //create the array
assert(list != NULL); //if unable to allocate memory
//space, terminate the program
for (int i = 0; i < length; i++)
list[i] = otherList.list[i];
}
return *this;
}
#endif
===========Main code is still the same================================================
=============================================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any
concern.