![Page 1: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/5.jpg)
The Stack Interface
![Page 6: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/6.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/7.jpg)
The Stack Interface
![Page 8: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/8.jpg)
The Stack Interface
![Page 9: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/11.jpg)
The Stack Interface
![Page 12: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/13.jpg)
The Stack Interface
![Page 14: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/14.jpg)
The Stack Interface
![Page 15: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/15.jpg)
The Stack Interface
![Page 16: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/16.jpg)
The Stack Interface
![Page 17: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/18.jpg)
The Stack Interface
![Page 19: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/19.jpg)
The Stack Interface
![Page 20: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/20.jpg)
The Stack Interface
![Page 21: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/22.jpg)
The Stack Interface
![Page 23: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/23.jpg)
The Stack Interface
![Page 24: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/24.jpg)
The Stack Interface
![Page 25: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/25.jpg)
The Stack Interface
![Page 26: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/26.jpg)
The Stack Interface
![Page 27: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/27.jpg)
The Call Stack
![Page 28: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/28.jpg)
The Call Stack
![Page 29: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/31.jpg)
The Call Stack
![Page 32: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/33.jpg)
The Call Stack
![Page 34: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/35.jpg)
The Queue Interface
![Page 36: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/36.jpg)
The Queue Interface
![Page 37: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/37.jpg)
The Queue Interface
![Page 38: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/38.jpg)
The Queue Interface
![Page 39: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/39.jpg)
The Queue Interface
![Page 40: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/40.jpg)
The Queue Interface
![Page 41: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/41.jpg)
The Queue Interface
![Page 42: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/42.jpg)
The Queue Interface
![Page 43: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/43.jpg)
The Queue Interface
![Page 44: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/44.jpg)
The Queue Interface
![Page 45: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/45.jpg)
The Queue Interface
![Page 46: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/46.jpg)
The Queue Interface
![Page 47: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/47.jpg)
The Queue Interface
![Page 48: Chapter 4 Stacks and Queues © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/48.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/49.jpg)
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](https://reader036.vdocuments.mx/reader036/viewer/2022070414/5697c00d1a28abf838cc982c/html5/thumbnails/50.jpg)
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.