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

__init__ and purge Methods

The definitions of the QueueAsArray class __init__ and purge methods are given in Program gif. In addition to self, the __init__ takes a single parameter, size, which specifies the maximum number of items that can be stored in the queue. The __init__ method initializes the instance attributes as follows: The _array instance attribute is initialized to an array of length _size and the _head and _tail instance attributes, are initialized to represent the empty queue. The total running time for the QueueAsArray __init__ method is O(n), 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 queue occupy contiguous array positions between _head and _tail. To empty the queue, the purge method walks through the occupied array positions assigning to each one the value None as it goes. 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.