Outline
Queues
queue interface
queue implementation: array queue
using queues
Queues
a stack is a Last-In, First-Out (LIFO) data structure
a First-In, First-Out (FIFO) data structure is known as a
queue
the word "queue" is used in the UK for what in the US is known as a "line", e.g. at a supermarket or a bank or a movie theater
the first person in the queue will be the first one served
queues are commonly used with computers:
documents to be printed are queued
packets to be sent on a network are queued
priority queues allow high-priority items to move to the head of a line
regular queues are strictly FIFO
queue interface
a queue is a collection of elements:
Interface Queue<E>
boolean isEmpty()
boolean offer(E value)
attempts to insert the value at the end of the queue, returning whether the insertion was completed
E poll()
removes and returns the object at the head of the queue, or
null
if the queue is empty
void remove()
is similar to
poll
, but throws an exception if the queue is empty
E peek()
is similar to
poll
, but does not remove the element
Iterator<E> iterator()
returns a new iterator over the elements of the queue
queue implementation strategies
similar to stack
linked list implementation
array implementation
as always, we don't need to know the type of the elements, only that they are objects
array implementation of queues
like stack: store all the elements in an array
when inserting a new element (
offer
), just like stack, add the element at the end of the queue
two choices when removing an new element from the front of the list (
poll
,
remove
):
copy all subsequent elements down by one index, so the first element is still at the beginning of the array, or
keep track of where the head of the queue is, with another index variable
in-class exercise: what are the advantages and disadvantages of these two strategies?
in-place array implementation
two integer variables, one the index of the head of the queue (
front
), the other the index of the tail of the queue (
end
)
so valid elements are found from
array[front]
through
array[end - 1]
queue contents get ever higher in the array, while lower-numbered indices will no longer contain valid data
eventually, even though the queue only has a limited amount of data, we will run out of indices to put new data into
solution: once we reach the end of the array, put the data beginning at index 0 again
the modulo operation can be used to make this simple:
index = (index + 1) % QUEUE_SIZE;
for example, if queue size is 15 and index = 14,
14 + 1 = 15,
15 % 15 = 0
so the new value of index is 0
the number of elements in the array is
(end - front + QUEUE_SIZE) % QUEUE_SIZE
,
but it is easier to simply maintain a
size
field
generic array allocation
creating a node to hold a reference to an object of type
T
is easy
creating an array to hold many reference to objects of type
T
cannot be done safely in Java
instead, have to declare:
private T array[] = (T[])new Object[MAX_SIZE];
and the compiler gives a warning
this is one of the (few) cases where the warning can be ignored
implementation of the method
offer
if there is room,
insert at
end
increment
end
modulo
MAX_SIZE
offer
calls the private method
full
to make sure room is available
if no room, simply returns
false
implementation in textbook (p. 322) doubles the size of the array
what is the runtime of this method?
implementation of the method
poll
if there is at least one element,
element is taken from
front
increment
front
modulo
MAX_SIZE
if no room, simply returns
null
peek
is even simpler, no increment, no size change
what is the runtime of this method?
what is the runtime of the
empty
method?
queue applications
simulating waiting lines:
random arrivals
random time to serve a client
single queue? multiple queues?
compute the average waiting time over many runs
recognizing palindromes:
level, racecar, radar, hannah, akasaka
queue is FIFO, stack is LIFO
push each character onto the stack, offer each character to the queue
then go through both data structures, the characters should be the same
in-class exercise (everyone together): do this for racecar, levels