The definitions of the StackAsArray constructor and purge methods 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. The constructor requires O(n) time to construct the array, where .
Program: StackAsArray class constructor and purge methods.
The purpose of the purge method is to remove all the contents of a container. In this case, the objects in the stack occupy the first count positions of the array. To empty the stack, the purge method simply assigns the value null to the first count positions of the array. Clearly, the running time for the purge method is O(n), where .