Data Structures and Algorithms with Object-Oriented Design Patterns in Python
next up previous index

__init__ and purge Methods

The definitions of the StackAsArray __init__ and purge methods are given in Program gif. In addition to self, the __init__ method 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 __init__ method requires O(n) time to construct the array, where tex2html_wrap_inline60687.

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 None to the first _count positions of the array. Clearly, the running time for the purge method is O(n), where tex2html_wrap_inline60691.


next up previous index

Bruno Copyright © 2003, 2004 by Bruno R. Preiss, P.Eng. All rights reserved.