Cover Data Structures and Algorithms with Object-Oriented Design Patterns in Java
next up previous contents index

The ``Head'' Methods

Program gif defines the methods enqueueHead and dequeueHead. The latter is trivial to implement--it simply calls the dequeue method inherited from the QueueAsArray class.

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 ContainerFullException exception is thrown when the deque is full. Next, the position at which to insert the new element is determined by decreasing the head field 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 thrown), the running time of enqueueHead is O(1).


next up previous contents index

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