|
Data Structures and Algorithms
with Object-Oriented Design Patterns in Python |
Program
defines the enqueueHead method.

Program: DequeAsArray class enqueueHead method.
In addition to self, the enqueueHead method takes a single argument which is the object to be added to the head of the deque. The enqueueHead method first checks that the deque is not full--a ContainerFull exception is raised when the deque is full. Next, the position at which to insert the new element is determined by decreasing the _head instance attribute by one modulo the length of the array. Finally, the object to be enqueued is put into the array at the correct position and the _count is adjusted accordingly. Under normal circumstances (i.e., when the exception is not raised), the running time of EnqueueHead is O(1).