Beginner C++: Templates & Ordered List question
I have an assignment to create an ordered list using templates so that it will accept all data types. I believe I have the template part of the program correct, but for some reason I can't get my list to come out in order. Here's my code so far:
Code:
#include <fstream>#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
template <class item>
class OrderedList
{
public:
OrderedList();
OrderedList(const OrderedList &);
~OrderedList();
int size();
void clear();
void add(const item newItem);
void remove(const int location);
item& operator[] (int);
private:
int currSize, maxSize;
item* data;
};
template <class item>
OrderedList<item>::OrderedList()
{
currSize = 0;
maxSize = 10;
}
template <class item>
OrderedList<item>::OrderedList(const OrderedList &a)
{
currSize = a.currSize;
maxSize = a.maxSize;
if (currSize > 0)
for(int k = 0; k < currSize; k++)
data[k] = a.data[k];
else
data = NULL;
}
template <class item>
OrderedList<item>::~OrderedList()
{
currSize = maxSize = 0;
if (data != NULL)
delete []data;
data = NULL;
}
template <class item>
int OrderedList<item>::size()
{
return currSize;
}
template <class item>
void OrderedList<item>::clear()
{
currSize = maxSize = 0;
if (data != NULL)
delete []data;
data = NULL;
}
template <class item>
void OrderedList<item>::add(const item newItem)
{
if(currSize >= maxSize)
maxSize +=5;
for(int k = 0; k < currSize; k++)
{
if(newItem < data[k])
{
for(int j = currSize; j >= k; j--)
data[j+1] = data[j];
data[k] = newItem;
currSize++;
return;
}
}
}
template <class item>
void OrderedList<item>::remove(const int location)
{
data[location] = NULL;
for(int k = location; k < currSize; k++)
data[k] = data[k+1];
currSize--;
return;
}
template <class item>
item& OrderedList<item>::operator[] (int a)
{
return data[a];
}
// After this is all code our instructor provided to test the program
struct testStruct
{
int iValue;
double dValue;
bool operator< (testStruct x) {return iValue < x.iValue; }
bool operator<=(testStruct x) {return iValue <= x.iValue; }
bool operator==(testStruct x) {return iValue == x.iValue; }
bool operator>=(testStruct x) {return iValue >= x.iValue; }
bool operator> (testStruct x) {return iValue > x.iValue; }
bool operator!=(testStruct x) {return iValue != x.iValue; }
};
int main()
{
ofstream out ("Project6.txt");
// Start off easily with an ordered list of integers
OrderedList<int> intList;
int value;
for (int k = 0; k < 25; k++)
{
value = rand() % 1000;
out << setw(5) << value;
if ((k+1) % 5 == 0) out << endl;
intList.add(value);
}
out << endl << endl;
for (int k = 0; k < intList.size(); k++)
{
value = intList[k];
out << setw(5) << value;
if ((k+1) % 5 == 0) out << endl;
}
out << endl << endl;
// Now work with an ordered list of doubles
OrderedList<double> doubleList;
double dValue;
out << fixed << showpoint << setprecision(2);
for (int k = 0; k < 25; k++)
{
dValue = rand() % 1000 + (0.01 * (rand() % 100));
out << setw(8) << dValue;
if ((k+1) % 5 == 0) out << endl;
doubleList.add(dValue);
}
out << endl << endl;
for (int k = 0; k < doubleList.size(); k++)
{
dValue = doubleList[k];
out << setw(8) << dValue;
if ((k+1) % 5 == 0) out << endl;
}
out << endl << endl;
// Now get fancy and create an ordered list of structures!
/* OrderedList <testStruct> structList;
testStruct sValue;
for (int k = 0; k < 25; k++)
{
sValue.iValue = rand() % 1000;
sValue.dValue = rand() % 1000 + (0.01 * (rand() % 100));
out << setw(5) << sValue.iValue << " " << setw(8) << sValue.dValue;
if ((k+1) % 2 == 0) out << endl;
structList.add(sValue);
}
out << endl << endl;
for (int k = 0; k < structList.size(); k++)
{
sValue = structList[k];
out << setw(5) << sValue.iValue << " " << setw(8) << sValue.dValue;
if ((k+1) % 2 == 0) out << endl;
}
*/
out << endl;
return 0;
}
My result "Project6.txt" prints out:
Code:
41 467 334 500 169
724 478 358 962 464
705 145 281 827 961
491 995 942 827 436
391 604 902 153 292
382.21 716.18 895.47 726.71 538.69
912.67 299.35 894.03 811.22 333.73
664.41 711.53 868.47 644.62 757.37
859.23 741.29 778.16 35.90 842.88
106.40 942.64 648.46 805.90 729.70
As you can tell, it's not very ordered... Can anyone point out any mistakes I may have made?
Thank you in advance,
Nick