Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC200 Week05 Stacks. www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface.

Similar presentations


Presentation on theme: "ITEC200 Week05 Stacks. www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface."— Presentation transcript:

1 ITEC200 Week05 Stacks

2 www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week05 Stacks (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

3 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

4 www.ics.mq.edu.au/ppdp 4 Specification of the Stack Abstract Data Type

5 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”

6 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)

7 www.ics.mq.edu.au/ppdp 7 Algorithm for Balanced Parenthesis Checking

8 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 =

9 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 –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

10 www.ics.mq.edu.au/ppdp 10 Implementing a Stack Using an Array

11 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

12 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

13 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 –

14 www.ics.mq.edu.au/ppdp 14 Case Study I: Evaluating Postfix Expressions

15 www.ics.mq.edu.au/ppdp 15 Evaluating Postfix Expressions (continued)

16 www.ics.mq.edu.au/ppdp 16 Evaluating Postfix Expressions (continued)

17 www.ics.mq.edu.au/ppdp 17 Evaluating Postfix Expressions (continued)

18 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 * 2w 5.1 sum / 2 * – Infix Postfix

19 www.ics.mq.edu.au/ppdp 19 Converting Infix to Postfix (continued)

20 www.ics.mq.edu.au/ppdp 20 Converting from Infix to Postfix (continued)

21 www.ics.mq.edu.au/ppdp 21 Converting from Infix to Postfix (continued)

22 www.ics.mq.edu.au/ppdp 22 Converting from Infix to Postfix (continued)

23 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

24 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


Download ppt "ITEC200 Week05 Stacks. www.ics.mq.edu.au/ppdp 2 Learning Objectives – Week05 Stacks (Chapter05) Students can Use the methods provided in the public interface."

Similar presentations


Ads by Google