next up previous index
Next: Interface Up: Simple Implementation Previous: Simple Implementation   Index

Methods

The constructor SimpleQueue again asks for a queue capacity, constructs an array of that size, and sets the queue size and pointers to 0.

The isEmpty and isFull methods are similar to those for stacks, except now we now refer to the number of items in the queue explicitly by using size. Previously top gave not only the place to write, but also the number of items on the stack.

The details of enqueue and dequeue are clear on inspection. The only points to comment on are the lines

if (top == queue.length) {
    top = 0;
}
and the similar one for base. An equivalent expression would be
top %= queue.length;
meaning that top is reassigned its remainder modulo queue.length. Since we know that top is only incremented by 1 on each occasion, the first expression covers the only case that can arise. Since it only involves an equality comparison, and no arithmetic, there is a significant efficiency gain.



Peter Williams 2005-06-07