For our lab 1 our professor has given us an assignment to create a doubly-linked list template class. He is actually calling it a database, but it's really a doubly linked list. I've finished the assignment, but thinking about it I've realized that I don't really like his requirements/design for this class.
His assignments states that we need to create a template class (named UBArray for UnBounded Array). Two template arguments will be used, (a) the first will be the data type to be stored, (b) the second is the data type of the index of the array (default to integer). Allow an unlimited number of elements. Elements will not be created unless they are used. A subscript operator [], and a function At which operates in the same way as the subscript operator. Also, the usual member functions such as GetFirst, GetLast, Remove, etc.
My problems is this: The data type that's chosen for the Index needs to support comparison operators because the list is sorted based on the value of the Index, not the Data. Is that wise? Of all the examples I've seen on the net, none of them are designed this way. So in reality we get two types of data in the Node, one is used for sorting and the other is the actual data that we need to be stored. Shouldn't we just get rid of Index?
Code:
//UBArray<DATA, INDEX>
//DATA &At(const INDEX &)
UBArray<double, int> uB;
for(int i = 0; i < 100; i++){
cout << uB.At(i) << endl;
}
The above code shows another problem, and I can't come up with a way to fix it without getting rid of Index argument. At() is called 99 times but no value is assigned to data, but memory gets allocated for each node. How can I make sure a constant version of At() is called when only reading the list so unnecessary allocation doesn't occur?