24
ITEC200 Week05 Stacks

ITEC200 Week05 Stacks. 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

ITEC200 Week05

Stacks

Page 2: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 3: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 4: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 4

Specification of the Stack Abstract Data Type

Page 5: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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”

Page 6: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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)

Page 7: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 7

Algorithm for Balanced Parenthesis Checking

Page 8: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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 =

Page 9: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 10: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 10

Implementing a Stack Using an Array

Page 11: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 12: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 13: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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 –

Page 14: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 14

Case Study I: Evaluating Postfix Expressions

Page 15: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 15

Evaluating Postfix Expressions (continued)

Page 16: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 16

Evaluating Postfix Expressions (continued)

Page 17: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 17

Evaluating Postfix Expressions (continued)

Page 18: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 19: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 19

Converting Infix to Postfix (continued)

Page 20: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 20

Converting from Infix to Postfix (continued)

Page 21: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 21

Converting from Infix to Postfix (continued)

Page 22: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

www.ics.mq.edu.au/ppdp 22

Converting from Infix to Postfix (continued)

Page 23: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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

Page 24: ITEC200 Week05 Stacks.  2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface

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