itec200 week05 stacks. 2 learning objectives – week05 stacks (chapter05) students can use the...
Post on 19-Dec-2015
217 views
TRANSCRIPT
ITEC200 Week05
Stacks
www.ics.mq.edu.au/ppdp 2
Learning Objectives – Week05Stacks (Chapter05)
Students can
• Use the methods provided in the public interface of the stack data type to manage a stack (push, pop, peek, and empty)
• Explain how Java implements stacks
• Analyse and augment various stack implementations (List, array, linked list)
• Identify problems requiring stacks and provide appropriate solutions
• Explain how detecting palindromes, testing for balanced (properly nested) parentheses, and evaluating arithmetic expressions (postfix and infix) can be accomplished using stacks and augment existing programs to perform related tasks
www.ics.mq.edu.au/ppdp 3
Stack Abstract Data Type
• A stack can be compared to a Pez dispenser– Only the top item can be accessed
– Can only extract one item at a time
• A stack is a data structure with the property that only the top element of the stack is accessible
• The stack’s storage policy is Last-In, First-Out
www.ics.mq.edu.au/ppdp 4
Specification of the Stack Abstract Data Type
www.ics.mq.edu.au/ppdp 5
Stack Application Example 1: Palindrome Finder
• Palindrome: string that reads the same in either direction, Such as: “I saw I was I”
www.ics.mq.edu.au/ppdp 6
Stack Application Example 2: Parentheses Matcher
• When analyzing arithmetic expressions, it is important to determine whether an expression is balanced with respect to parentheses– (a+b*(c/(d-e)))+(d/e)
www.ics.mq.edu.au/ppdp 7
Algorithm for Balanced Parenthesis Checking
www.ics.mq.edu.au/ppdp 8
Implementing a Stack as an Extension of Vector
• The Java API includes a Stack class as part of the package java.util
• The Stack class is implemented as an Extension of the Vector class.
• The Vector class is a List that houses a growable array of objects
s =
www.ics.mq.edu.au/ppdp 9
Implementing a Stack with a List Component
• Can use either the ArrayList, Vector, or the LinkedList classes as all implement the List interface
• Name of class illustrated in text is ListStack<E>– ListStack is an adapter class as it adapts the methods
available in another class to the interface its clients expect by giving different names to essentially the same operations
www.ics.mq.edu.au/ppdp 10
Implementing a Stack Using an Array
www.ics.mq.edu.au/ppdp 11
Implementing a Stack as a Linked Data Structure
• We can implement a stack using a linked list of nodes
www.ics.mq.edu.au/ppdp 12
Comparison of Stack Implementations
• Extending a Vector (as is done by Java) is a poor choice for stack implementation as all Vector methods are accessible
• Easiest implementation would be to use an ArrayList component for storing data
• All insertions and deletions are constant time regardless of the type of implementation discussed
www.ics.mq.edu.au/ppdp 13
Additional Stack Applications
• Two case studies that consider the application of stacks in depth:– Evaluating postfix arithmetic expressions
E.g., 4 7 * 20 – = 8)
– Converting arithmetic expressions from infix to postfix
E.g., ( 4 * 7 ) – 20 converts to 4 7 * 20 –
www.ics.mq.edu.au/ppdp 14
Case Study I: Evaluating Postfix Expressions
www.ics.mq.edu.au/ppdp 15
Evaluating Postfix Expressions (continued)
www.ics.mq.edu.au/ppdp 16
Evaluating Postfix Expressions (continued)
www.ics.mq.edu.au/ppdp 17
Evaluating Postfix Expressions (continued)
www.ics.mq.edu.au/ppdp 18
Case Study II: Converting Infix to Postfix
• Converting from Infix to Postfix facilitates the evaluation of infix expressions by arithmetic computational devices
w – 5.1 / sum * 2 w 5.1 sum / 2 * –
Infix Postfix
www.ics.mq.edu.au/ppdp 19
Converting Infix to Postfix (continued)
www.ics.mq.edu.au/ppdp 20
Converting from Infix to Postfix (continued)
www.ics.mq.edu.au/ppdp 21
Converting from Infix to Postfix (continued)
www.ics.mq.edu.au/ppdp 22
Converting from Infix to Postfix (continued)
www.ics.mq.edu.au/ppdp 23
Where to from here…
• Work through Chapter 5 of the Koffman & Wolfgang Text
• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class
www.ics.mq.edu.au/ppdp 24
Acknowledgements
These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 35PowerPoint presentation
by Elliot B. Koffman and Paul A. T. Wolfgang