Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights...

50
Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Transcript of Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights...

Page 1: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Chapter 4

Stacks and Queues

© 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Page 2: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview

● Stacks and Queues – Data structure holding a collection of objects,

ordered by when they were inserted into the structure.

– Stack can access newest object.– Queue can access oldest object.

Page 3: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Overview● 4.1

– The Stack Interface● Stack behavior● Generic classes

● 4.2 – The Java Call Stack

● 4.4– The Queue Interface

Page 4: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface● Stack

– Data structure holding several items.– New items added to the top of the stack.

● Pushing– Push items onto the stack

● Pop– Pop items off of the stack or extract the topmost

item.● Peek

– Peek at the top item or check if the stack is empty.

Page 5: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 6: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

● Iast-in, first-out or LIFO policy– Last item pushed on the stack is next item popped

off the stack.– Example:

● A stack of plates.

Page 7: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 8: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 9: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

● Generics– The Stack interface describes a stack of Objects.

● s.push(new Die(3));– When we pop a Stack we get back an Object.– We usually cast it to a more specific class so we

can use specific functionality.● ((Die)(s.pop())).roll();

– This is not only inconvenient, it is slightly unsafe.– We must remember what we put on the Stack, or

we might try to cast something that is not really a Die to be a Die.

Page 10: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface● Generic classes and interfaces

– Has one or more type parameters.– We have to specify a type for each type of

parameter.● Stack<Die> s = new Stack<Die>():

● s.push(new Die());

s.pop().roll();– Java won't let us push anything of the wrong type

onto this Stack.– The pop() method's return type is specified by the

type parameter.

Page 11: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 12: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

● In UML diagrams type parameters are shown as dashed boxes are at the upper right of a class.

Page 13: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 14: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 15: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 16: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 17: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

● Generics do not play well with arrays– We can declare an array field or variable involving

type parameters, like the field stacks.– We cannot actually allocate an array involving a

type parameter.● Example: stacks = new Stack<Card>[4]

– Assigning each cell in the array a generic type parameter produces a warning, but not an error.

Page 18: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 19: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 20: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 21: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

● The pop() method returns a value, but we don't use that value in this particular program.

Page 22: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 23: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 24: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 25: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 26: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Stack Interface

Page 27: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

Page 28: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

Page 29: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

● The hypotenuse() method invokes the square() method twice.

● How does Java know where to go when it finishes an invocation of square()?

● Java keeps track of what it was doing before the invocation started by using a stack.– This stack is called a call stack.

Page 30: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

● Every time a method is invoked, a call frame is created.– The call frame keeps track of the current state of

the method.– It stores any arguments or variables for the method.– It also keeps track of how far along the method has

proceeded.

Page 31: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

Page 32: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

● Knowledge of the call stack helps us understand some of Java's error messages.

● The error message shows a stack trace (a snapshot of the call stack) indicating what was going on when the program crashed.

Page 33: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Call Stack

Page 34: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

● Queue– Very similar to a stack.– Items are inserted in one end (the back) and

removed from the other end (the front).– first-in, first-out, or FIFO

Page 35: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 36: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 37: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 38: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 39: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 40: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 41: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 42: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 43: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 44: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 45: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 46: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 47: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

The Queue Interface

Page 48: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Summary

● Stacks and queues are collections of objects.– Stack follows a last-in, first-out policy.

● Objects are pushed onto and popped off the top.– Queue follows a first-in, first-out policy.

● Objects are added to the back and removed from the front.

Page 49: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Summary

● Java 1.5 introduces generic types and interfaces.– Generics greatly reduces the amount of casting we

have to do when using general-purpose data structures.

Page 50: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.

Summary

● Call Stack– Whenever a method is invoked, a new call frame is

pushed onto the stack.● This keeps track of any variables in the method, as well

as how far the method has progressed.