Java Methods ObjectOriented Programming and Data Structures 3
Java Methods Object-Oriented Programming and Data Structures 3 rd AP edition Maria Litvin ● Gary Litvin Stacks and Queues Copyright © 2015 by Maria Litvin, Gary Litvin, and Skylight Publishing. All rights 1 reserved.
Objectives: • Discuss different implementations of stacks and queues • Learn about applications of stacks and queues 2
Stack Queue pop push remove add LIFO (Last-In-First-Out) access method FIFO (First-In-First-Out) access method Stacks and queues are used for temporary storage, but in different situations 3
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 4
Stack: Array Implementation public void push (Object x) { my. Elements [sp] = x; sp++; } public Object pop ( ) { sp--; return my. Elements [sp]; } 5
Array. List Implementation import java. util. Array. List; public class Array. Stack { private Array. List<Object> items; public Array. Stack ( ) { items = new Array. List<Object>( ); } public boolean is. Empty ( ) { return items. is. Empty ( ); } 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); } } 6
Linked. List Implementation import java. util. Linked. List; public class List. Stack { private Linked. List<Object> items; public List. Stack () { items = new Linked. List<Object> ( ); } public boolean is. Empty ( ) { return items. is. Empty ( ); } public void push (Object x) { items. add. First (x); } public Object pop ( ) { return items. remove. First ( ); } public Object peek ( ) { return items. get. First ( ); } } 7
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). 8
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 Array. List. • Methods: is. Empty, push, pop, peek. • Has other methods do not use them! 9
Stack Example: Matching Brackets public boolean brackets. Match (String str) import java. util. Stack; { Stack<Integer> stk = new Stack<Integer> ( ); for (int pos = 0; pos < str. length( ); pos++) { if (str. char. At (pos) == ' [ ' ) ) stk. push (pos); Autoboxing else if (str. char. At (pos) == ' ] ' )) (Save pos of ' [ ‘) { if (stk. is. Empty ( )) return false; Autounboxing int pos 0 = stk. pop ( ); System. out. println (pos 0 + " - " + pos); } } return stk. is. Empty ( ); } 10
Stack Example: Traversing a Tree Stack stk = new Stack<Tree. Node>( ); Tree. Node node = root; while (node != null) { System. out. println (node. get. Value ( )); if (node. get. Left ( ) != null ) { if (node. get. Right ( ) != null ) stk. push (node. get. Right ( )); Save for future node = node. get. Left ( ); processing } else if (node. get. Right ( ) != null ) node = node. get. Right ( ); else if (! stk. is. Empty ( )) node = stk. pop ( ); if no children, else take the next node = null; node from the } stack 11
Queues are used for: • Processing events or messages in order of their arrival • System tasks Ø Ø Ø Queueing print jobs Entering keystrokes Processing mouse clicks 12
Queue: Ring-Buffer Implementation 13
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). 14
The java. util. Queue Interface boolean is. Empty () boolean add (E obj) E remove () E peek () • A “generic” interface, part of the Java Collections Framework (Chapter 20) • Implemented by java. util. Linked. List 15
Queue Example public Queue<String> find. Matches (Scanner input, String target) { Queue<String> q = new Linked. List<String>( ); while (input. has. Next. Line ( )) { Returns a queue of String line = input. next. Line ( ); all the lines that if (line. index. Of (target) >= 0 ) contain target q. add (line); } public void process (Queue<String> q) return q; { } while (! q. is. Empty ( )) { Processes the String s = q. remove ( ); contents of q. . . // process s (leaves the } queue empty) } 16
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. 17
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 Array. List or a Linked. List. • Why is an Array. List not as efficient for implementing a queue (unless you use a ring-buffer implementation)? 18
- Slides: 18