The definitions of the StackAsArray class constructor
and destructor are given in Program .
The constructor takes a single parameter, size,
which specifies the maximum number of items that can be stored in the stack.
The variable array is initialized to be an array of length size.
Its constructor requires O(1) time to construct an array of pointers.
Therefore, the total running time for the StackAsArray
constructor is O(1).
Program: StackAsArray Class Constructor, Destructor and Purge Member Function Definitions
The StackAsArray destructor
simply calls the Purge member function.
The behavior of the Purge function
depends on whether the stack (as a container) is the owner
of the contained objects.
If it is the owner,
then the contained objects must be deleted by the Purge function.
In this case, the Purge function deletes one-by-one
the first count elements of the array.
In general, because of the polymorphic implementation of Objects,
we cannot know the running time to delete the array elements.
However, if we assume that the destructors for all the objects
each run in constant time,
the total running time for the Purge function is O(n),
where ,
i.e., n is the number of elements in the stack.