Queues

Queues
From last time: Java's built-in
java.util.Stack class
Consider the javadoc for Java’s built-in java.util.Stack
class
(http://download.oracle.com/javase/7/docs/api/java/u
til/Stack.html).
What are the design flaws in this class? Explain.
Hint: can it guarantee that the item you pop will always
be the last item pushed?
Queues p. 2/19
Overview
 What are queues useful for?
 The QueueADT interface
 LinkedQueue implementation
 Time complexity of LinkedQueue operations
 Array implementations
 Application: ticket counter
 Queues vs. stacks
 Coming attractions
Queues p. 3/19
What are queues useful for?
Suppose we want to model:
A.Customers waiting in line for an online helpdesk
B.Print jobs waiting for the printer
C.Customers waiting in line at a bank
Assuming no one cuts in line, what operations would the
solutions to these problems have in common?
Queues p. 4/19
Now contrast the following:
A. The undo operation in Word
B. The Back button in a web browser
C. A hundred names in alphabetical order, where
names are added and removed frequently
D. The roads and intersections in a city
Would our queue operations help to solve these
problems? Why or why not?
Queues p. 5/19
You try …
Give three everyday examples of a situation that could be
modeled using a queue.
Queues p. 6/19
The QueueADT interface
 Consider the code p. 102
 Compare the operations given here with those we
listed.
Queues p. 7/19
The QueueADT interface
In the code on page 102, the capital letter “T” stands for:
A. a temporary value
B. the type of the items in the Queue
C. a class named T defined somewhere else in the
program
D. the top of the Queue
E. none of the above
Queues p. 8/19
A LinkedQueue implementation
Consider the start of the implementation on the top of
page 114.
What's the overall purpose?
And what is each line there for?
Queues p. 9/19
A LinkedQueue implementation
Consider the LinkedQueue code on pp. 114-116.
What happens when client code tries to enqueue an
item?
Describe precisely with box and arrow diagrams and
reference to particular lines of code.
Queues p. 10/19
Time Complexity of LinkedQueue
Consider the LinkedQueue code on pp. 114-116.
The Big-Oh time complexity of the enqueue operation
is:
A. O(1)
B. O(log2 n)
C. O(n)
D. O(n2)
E. none of the above
Queues p. 11/19
Time Complexity of LinkedQueue
Consider the LinkedQueue code on pp. 114-116.
The Big-Oh time complexity of the dequeue operation
is:
A. O(1)
B. O(log2 n)
C. O(n)
D. O(n2)
E. none of the above
Queues p. 12/19
Time Complexity of LinkedQueue
Consider the LinkedQueue code on pp. 114-116.
What is the Big-Oh time complexity of the other Queue
operations?
Queues p. 13/19
Array implementations
 Draw a picture of an 8-element array.
 Insert the characters A, B, C, D, E into the first five
locations of the array.
 Suppose A is the front of a queue and E is the rear.
 Dequeue 3 elements and then add 4.
 What happens?
Queues p. 14/19
Possible solution: “circular”
arrays
 How would these solve our problem? Describe
precisely using the example above.
 How do they compare with the linked list
implementation of queues? What are the advantages
and limitations of each?
Queues p. 15/19
Application: ticket counter
(In groups)
Consider the ticket counter simulation problem and the
solution given in the text (pp. 107-112).
Hand trace the solution for 22 customers and 4 cashiers.
Queues p. 16/19
Queues vs. stacks
Match each stack operation with the related queue
operation:
A. push
1. isEmpty
B. pop
2. enqueue
C. peek
3. dequeue
D. isEmpty 4 toString
E. toString 5. front
Queues p. 17/19
Queues vs. stacks
Which operations work the same even though they may
have different names? Which work differently?
(Answer with reference to specific lines of code. If no
code is given in the book, explain how you would
implement the operation).
A. push
1. isEmpty
B. pop
2. enqueue
C. peek
3. dequeue
D. isEmpty 4 toString
E. toString 5. front
Queues p. 18/19
Coming attractions
 Next time we'll look at our third Abstract Datatype,
ListADT, a new linear data structure that allows
elements to be added at the beginning, the end, and
the middle.
 Homework: read Chapter 6 Lists (or the equivalent in
the earlier edition).
Queues p. 19/19