Java Reference
In-Depth Information
4.
The
put( )
method, which stores elements, is shown next:
The method begins by checking for a queue-full condition. If
putloc
is equal to one
past the last location in the
q
array, there is no more room in which to store elements.
Otherwise, the new element is stored at that location and
putloc
is incremented.
Thus,
putloc
is always the index where the next element will be stored.
5.
To retrieve elements, use the
get( )
method, shown next:
Notice first the check for queue-empty. If
getloc
and
putloc
both index the same ele-
ment, the queue is assumed to be empty. This is why
getloc
and
putloc
were both
initialized to zero by the
Queue
constructor. Then, the next element is returned. In
the process,
getloc
is incremented. Thus,
getloc
always indicates the location of the
next element to be retrieved.
6.
Here is the entire
QDemo.java
program: