I was given a template class code here and I have to add the code to a insert function in there, I have no real experience with templates, how would I go about doing this ?

here is the template class code.cpp

// Implementation of the templated version of arrayListType
#include <iostream>
#include <cassert>
#include "arrayListTypeT.h"

using namespace std;

template<class elemType>
bool arrayListTypeT<elemType>::isEmpty() const
{ return (length == 0); }

template<class elemType>
bool arrayListTypeT<elemType>::isFull() const
{ return (length == maxSize); }

template<class elemType>
int arrayListTypeT<elemType>::listSize() const
{ return length; }

template<class elemType>
int arrayListTypeT<elemType>::maxListSize() const
{ return maxSize; }

template<class elemType>
void arrayListTypeT<elemType>::print() const
{
	for (int i = 0; i < length; i++)
		cout << list[i] << " ";
	cout << endl;
}

template<class elemType>
bool arrayListTypeT<elemType>::isItemAtEqual(int location, elemType item) const
{ return(list[location] == item); }

template<class elemType>
void arrayListTypeT<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 arrayListTypeT<elemType>::retrieveAt(int location, elemType& retItem)
{
	if (location < 0 || location >= length)
    	cout << "The location of the item to be retrieved is "
			 << "out of range." << endl;
	else
		retItem = list[location];
} // retrieveAt

template<class elemType>
void arrayListTypeT<elemType>::replaceAt(int location, elemType repItem)
{
	if (location < 0 || location >= length)
    	cout << "The location of the item to be replaced is "
			 << "out of range." << endl;
	else
		list[location] = repItem;

} //end replaceAt

template<class elemType>
void arrayListTypeT<elemType>::clearList()
{
	length = 0;
} // end clearList

template<class elemType>
arrayListTypeT<elemType>::arrayListTypeT<elemType>(int size)
{
    if (size <= 0)
    {
        cout << "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>
arrayListTypeT<elemType>::~arrayListTypeT<elemType>()
{ delete [] list; }

template<class elemType>
arrayListTypeT<elemType>::arrayListTypeT<elemType>(const arrayListTypeT<elemType>& otherList)
{
    maxSize = otherList.maxSize;
    length = otherList.length;
    list = new elemType[maxSize];  //create the array
    assert(list != NULL);          //terminate if unable to allocate

    for (int j = 0; j < length; j++)  //copy otherList
        list [j] = otherList.list[j];
}//end copy constructor

template<class elemType>
int arrayListTypeT<elemType>::seqSearch(elemType item)  const
{
	int i;
	for (i = length - 1; i >= 0; i--)
	   if (list[i] == item)
	   { break; }
	return i;
} //end seqSearch

template<class elemType>
void arrayListTypeT<elemType>::insert(elemType insertItem)
{
  // Your code to insert a new item into the list goes here.
  // You are required to check for the following errors:
  // Duplicate insertion.
  // Full list.
  // If either error occurs, display an appropriate message leave the 
  // list unchanged.
} //end insert

template<class elemType>
void arrayListTypeT<elemType>::remove(elemType removeItem)
{
	int loc;
	if (length == 0)
		cout << "Cannot delete from an empty list." << endl;
	else
	{
		loc = seqSearch(removeItem);

		if (loc != -1)
			removeAt(loc);
		else
			cout << "The team to be deleted is not in the list."
				 << endl;
	}
} //end remove

Here is the insert function

void arrayListTypeT<elemType>::insert(elemType insertItem)
{
  // Your code to insert a new item into the list goes here.
  // You are required to check for the following errors:
  // Duplicate insertion.
  // Full list.
  // If either error occurs, display an appropriate message leave the 
  // list unchanged.
} //end insert

We were told not to modify any part of this class besides the insert function

Recommended Answers

All 3 Replies

Is all you're needing to do is insert values into an array? If so all you would have to do is..

int x = value;
a[0] = x;

Of course you would have to have some way of keeping track of the appropriate index for the array.

As for checking to see if the item being inserted is already in the list you could do either a basic linear search or binary search.

For the full list you just have to check if each element in the array contains a value.

my man concern is inserting the item in ascending order.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.