Download presentation
Presentation is loading. Please wait.
Published bySpencer Summers Modified over 8 years ago
1
Stacks and Queues Copyright © 2011 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights reserved. Java Methods Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin 2nd AP edition with GridWorld
2
22-2 Objectives: Discuss different implementations of stacks and queues Learn about applications of stacks and queues
3
22-3 push pop StackQueue LIFO (Last-In-First-Out) access method add remove FIFO (First-In-First-Out) access method Stacks and queues are used for temporary storage, but in different situations
4
22-4 Stacks are Used for handling nested structures: processing directories within directories evaluating expressions within expressions handling branching processes: traversing a branching tree structure planning a move in a chess game tracking the sequence of method calls in a Java program
5
22-5 Stack: Array Implementation public void push (Object x) { myElements [sp] = x; sp++; } public Object pop ( ) { sp -- ; return myElements [sp]; }
6
22-6 ArrayList Implementation import java.util.ArrayList; public class ArrayStack { private ArrayList items; public ArrayStack ( ) { items = new ArrayList ( ); } public boolean isEmpty ( ) { return items.isEmpty ( ); } public void push (Object x) { items.add (x); } public Object pop ( ) { return items.remove (items.size ( ) - 1); } public Object peek ( ) { return items.get (items.size ( ) - 1); } }
7
22-7 LinkedList Implementation import java.util.LinkedList; public class ListStack { private LinkedList items; public ListStack () { items = new LinkedList ( ); } public boolean isEmpty ( ) { return items.isEmpty ( ); } public void push (Object x) { items.addFirst (x); } public Object pop ( ) { return items.removeFirst ( ); } public Object peek ( ) { return items.getFirst ( ); } }
8
22-8 Properties of Stacks In an efficient implementation, push, pop, and peek methods run in O(1) time. A stack of objects holds references to objects. If necessary, a stack can hold multiple references to the same object. If you are not careful, an object can change while stored on the stack (unless that object is immutable).
9
22-9 java.util.Stack class Part of the Java Collections Framework (Chapter 20). A “generic” class (works with objects of specified type). Based on the legacy Vector class, similar to ArrayList. Methods: isEmpty, push, pop, peek. Has other methods do not use them!
10
22-10 Stack Example: Matching Brackets public boolean bracketsMatch (String str) { Stack stk = new Stack ( ); for (int pos = 0; pos < str.length( ); pos++) { if (str.charAt (pos) == ' [ ' ) ) stk.push (pos); else if (str.charAt (pos) == ' ] ' )) { if (stk.isEmpty ( )) return false; int pos0 = stk.pop ( ); System.out.println (pos0 + " - " + pos); } return stk.isEmpty ( ); } Autounboxing Autoboxing (Save pos of ' [ ‘) import java.util.Stack;
11
22-11 Stack Example: Traversing a Tree Stack stk = new Stack ( ); TreeNode node = root; while (node != null) { System.out.println (node.getValue ( )); if (node.getLeft ( ) != null ) { if (node.getRight ( ) != null ) stk.push (node.getRight ( )); node = node.getLeft ( ); } else if (node.getRight ( ) != null ) node = node.getRight ( ); else if (! stk.isEmpty ( )) node = stk.pop ( ); else node = null; } Save for future processing if no children, take the next node from the stack
12
22-12 Queues are used for: Processing events or messages in order of their arrival System tasks Queueing print jobs Entering keystrokes Processing mouse clicks
13
22-13 Queue: Ring-Buffer Implementation
14
22-14 Properties of Queues In an efficient implementation, add, remove, and peek methods run in O(1) time. A queue of objects holds references to objects. If necessary, a queue can hold multiple references to the same object. If you are not careful, an object can change while stored in the queue (unless that object is immutable).
15
22-15 The java.util.Queue Interface A “generic” interface, part of the Java Collections Framework (Chapter 20) Implemented by java.util.LinkedList boolean isEmpty () boolean add (E obj) E remove () E peek ()
16
22-16 Queue Example public Queue findMatches (Scanner input, String target) { Queue q = new LinkedList ( ); while (input.hasNextLine ( )) { String line = input.nextLine ( ); if (line.indexOf (target) >= 0 ) q.add (line); } return q; } Returns a queue of all the lines that contain target public void process (Queue q) { while (! q.isEmpty ( )) { String s = q.remove ( );... // process s } Processes the contents of q (leaves the queue empty)
17
22-17 Review: What are the two main operations for a stack? Name a few applications of stacks. Name the four methods of the java.util.Stack class. What are the two main operations for a queue? Name a few applications of queues.
18
22-18 Review (cont’d): Name the four methods of the java.util.Queue interface. Explain why a stack of objects can be equally efficiently implemented using an ArrayList or a LinkedList. Why is an ArrayList not as efficient for implementing a queue (unless you use a ring-buffer implementation)?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.