Data Structures, Algorithms, & Applications in C++
Chapter 5, Exercise 11
The code is given below and in the file
arrayListWithPush.h. Test code is
included in arrayListWithPush.cpp.
template<class T>
class arrayListWithPush : public arrayList<T>
{
public:
// constructor and destructor
arrayListWithPush(int initialCapacity = 10)
: arrayList<T> (initialCapacity) {}
void push_back(const T& theElement);
};
template<class T>
void arrayListWithPush<T>::push_back(const T& theElement)
{// Insert theElement at right end of list.
// make sure we have space
if (listSize == arrayLength)
{// no space, double capacity
changeLength1D(element, arrayLength, 2 * arrayLength);
arrayLength *= 2;
}
element[listSize] = theElement;
listSize++;
}
The complexity of the function is O(1) when the array
element doesn't need to be resized; otherwise, the
complexity is O(listSize).