Professional Documents
Culture Documents
Queue
boolean add(E e)
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions, returning true upon
success and throwing an IllegalStateException if no space is currently
available.
E element()
Retrieves, but does not remove, the head of this queue.
boolean offer(E e)
Inserts the specified element into this queue if it is possible to do so
immediately without violating capacity restrictions.
E peek()
Retrieves, but does not remove, the head of this queue, or returns null if
this queue is empty.
E poll()
Retrieves and removes the head of this queue, or returns null if this
queue is empty.
E remove()
Retrieves and removes the head of this queue.
Q
0 1 2 f r
Q
0 1 2 r f
Q
0 1 2 f r
Q
0 1 2 r f
Algorithm dequeue()
Operation dequeue if isEmpty() then
throws an exception throw EmptyQueueException
if the queue is empty else
o Q[f]
This exception is f (f + 1) mod N
specified in the return o
queue ADT
Q
0 1 2 f r
Q
0 1 2 r f
TSE3124 - DATA STRUCTURES
TSE3124 - DATA STRUCTURES
Application of Queues
Operating systems:
queue of print jobs to send to the printer
queue of programs / processes to be run
queue of network data packets to send
Programming:
modeling a line of customers or clients
storing a queue of computations to be performed in order
The Queue
Shared
Service
System.out.println(q.remove()); // 42
IMPORTANT: When constructing a queue you must use a new LinkedList object instead of a new Queue
object.
This has to do with a topic we'll discuss later called interfaces. TSE3124 - DATA
STRUCTURES
Queue Interface in Java
while (!q.isEmpty()) {
do something with q.remove();
}
another idiom: Examining each element exactly once.
System.out.println(q); // [3, 2, 1]