queues. from last time: java's built-in java.util.stack class consider the javadoc for java’s...

19
Queues

Post on 21-Dec-2015

228 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Queues

Page 2: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

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/util/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

Page 3: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

OverviewWhat are queues useful for?The QueueADT interfaceLinkedQueue implementationTime complexity of LinkedQueue operationsArray implementationsApplication: ticket counterQueues vs. stacksComing attractions

Queues p. 3/19

Page 4: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

What are queues useful for?Suppose we want to model:A.Customers waiting in line for an online helpdeskB.Print jobs waiting for the printerC.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

Page 5: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Now contrast the following:A. The undo operation in WordB. The Back button in a web browserC. A hundred names in alphabetical order, where

names are added and removed frequentlyD. The roads and intersections in a city

Would our queue operations help to solve these problems? Why or why not?

Queues p. 5/19

Page 6: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

You try …Give three everyday examples of a situation that

could be modeled using a queue.

Queues p. 6/19

Page 7: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

The QueueADT interfaceConsider the code p. 102Compare the operations given here with

those we listed.

Queues p. 7/19

Page 8: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

The QueueADT interfaceIn 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

Page 9: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

A LinkedQueue implementationConsider 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

Page 10: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

A LinkedQueue implementationConsider 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

Page 11: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Time Complexity of LinkedQueueConsider 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

Page 12: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Time Complexity of LinkedQueueConsider 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

Page 13: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Time Complexity of LinkedQueueConsider the LinkedQueue code on pp. 114-116. What is the Big-Oh time complexity of the other

Queue operations?

Queues p. 13/19

Page 14: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Array implementationsDraw 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

Page 15: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Possible solution: “circular” arraysHow 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

Page 16: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

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

Page 17: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Queues vs. stacksMatch each stack operation with the related

queue operation:A. push 1. isEmptyB. pop 2. enqueueC. peek 3. dequeueD. isEmpty 4 toStringE. toString 5. front

Queues p. 17/19

Page 18: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Queues vs. stacksWhich 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. isEmptyB. pop 2. enqueueC. peek 3. dequeueD. isEmpty 4 toStringE. toString 5. front

Queues p. 18/19

Page 19: Queues. From last time: Java's built-in java.util.Stack class Consider the javadoc for Java’s built-in java.util.Stack class (

Coming attractionsNext 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