chapter 7 stack. overview ● the stack data structure uses an underlying linear storage...

47
Chapter 7 Stack

Upload: nancy-willis

Post on 28-Dec-2015

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Chapter 7

Stack

Page 2: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Overview

● The stack data structure uses an underlying linear storage organization. The stack is one of the most ubiquitous data

structures in computing.

Page 3: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Learning Objectives

● Describe the behavior of a stack.● Enumerate the primary operations supported by

the stack.● Examine several applications of the stack,

including parentheses matching, evaluating postfix expressions, and the conversion of an infix expression to postfix form.

● Understand the public interface of a stack class in Java and the running times of its methods.

Page 4: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Learning Objectives

● Develop a postfix package in Java to implement postfix expression evaluation.

● Study the implementation of a stack class in Java, and the trade-offs involved in choosing between candidate reusable components.

Page 5: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.1 Stack Properties

● Surfing the Web on a browser: The sequence of back clicks loads the browser with

Web pages in reverse order of visit. The last visited page is the first loaded when going

back.● A stack is a collection of entries that

demonstrates exactly this last in, first out behavior, called LIFO in short.

Page 6: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.1 Stack Properties

Page 7: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.1 Stack Properties

● An entry added or pushes on to the top of a stack.

● An entry is removed, or popped from the top of stack.

Page 8: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.1 Stack Properties

Page 9: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of Stack Operations Using StackNode (Cont’d)

Internal View Abstract View

Page 10: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of Stack Operations Using StackNode (Cont’d)

Internal View Abstract View

Page 11: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of Stack Operations Using StackNode (Cont’d)

Internal View Abstract View

Page 12: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of Stack Operations Using StackNode (Cont’d)

Internal View Abstract View

Page 13: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of push Operation

Page 14: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of push Operation (Cont’d)

Page 15: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of push Operation (Cont’d)

Page 16: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of push Operation (Cont’d)

Page 17: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

Results of pop Operation

top = top.next

Page 18: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

● We write arithmetic expressions like so:

● Consider the expression:

● It cannot simply be scan left to right.

Page 19: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

● Postfix, does away with the need for parentheses.

● An operator always follows the operands or sub-expressions on which it operates.

Page 20: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

Page 21: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

Page 22: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

● Two conditions that must be met for the evaluation process to terminate successfully: When an operator is encountered, there must exist

a most recent pair of operands or temporary results for application.

When the scanning of the expression is complete, there must be exactly one value on the stack.

Page 23: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

Page 24: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.2.2 Postfix Expression Evaluation

● Two possible errors that may be encountered by the algorithm: One is that of insufficient operands. The other is that of too many operands.

● Insufficient operands case is detected when the token is an operator, but the stack has less than the two operands on which the operator must be applied.

● Too many operands case is detected after the while loop, when the stack has more than one entry in it.

Page 25: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.3 A Stack Class

Page 26: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.3 A Stack Class

Page 27: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4 A Postfix Expression Evaluation Package

● Every step of the evaluation processes one token of the expression.

Page 28: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.1 Class PostfixEvaluator

Page 29: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.1 Class PostfixEvaluator

java.util.StringTokenizer parses the postfix expression into tokens and deals them out one at a time.

StackKeeper maintains the evaluation stack.

Page 30: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.1 Class PostfixEvaluator

Page 31: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.1 Class PostfixEvaluator

● RunAll message evaluates and produces the results in one shot.

● Restart the evaluator by the init message.

Page 32: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.3 Class StackKeeper

Page 33: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.3 Class StackKeeper

Page 34: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.3 Class StackKeeper

Page 35: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.3 Class StackKeeper

Page 36: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.4 Class PostfixEvaluator Implementation

Page 37: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.4 Class PostfixEvaluator Implementation

● The StringTokenizer method count Tokens returns the number of tokens that remain to be enumerated. At the end of the run, the stack must contain exactly

one element.

Page 38: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.4.4 Class PostfixEvaluator Implementation

● The NoSuchElementException does two things: Prints the current evaluation status so the calling

application gets as much information as possible about the source of the exception.

Throws an IllegalExpressionException, in order to deliver the most precise and complete information about the cause of the exception.

● This is much better than just passing through the NoSuchElementException, which, in this context, is not informative enough.

Page 39: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.1 Design1: Array List for Storage

● Using an instance of the ArrayList class as a component in the Stack class.

Page 40: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.2 Design 2: Linked List for Storage

● Which end of the list should be used for the pushes and pops? Addition could be done at either end on O(1) time. Deletion from the front can be done in O(1) time,

deletion from the rear can only be done in O(n) time.

Page 41: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.2 Design 2: Linked List for Storage

Page 42: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.2 Design 2: Linked List for Storage

Page 43: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.2 Design 2: Linked List for Storage

Page 44: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.5.2 Design 2: Linked List for Storage

● The insertAt (item, index) method takes O(n) time in the worst case, but insertAt (item, 0) only times O(1) time.

● The removeAt (index) method takes O(n) time in the worst case, but removeAt (0) only takes O(1) time.

Page 45: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.6 Summary

● The stack data structure implements the Last In First Out (LIFO) abstraction.

● A stack is a linear collection of entires in which, for every entry y that enters the stack after another entry x, y leaves the stack before x.

● A stack memorizes things and recalls them in reverse order.

● There are two fundamental operations supported by a stack: push and pop.

Page 46: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.6 Summary

● Every infix expression can be written unambiguously in postfix form.

● A postfix expression can be evaluated by using a stack with a single left-to-right scan of the expression.

● A stack class in Java may define more than just the fundamental push and pop methods in its interface in order to provide better efficiency and ease of use.

Page 47: Chapter 7 Stack. Overview ● The stack data structure uses an underlying linear storage organization.  The stack is one of the most ubiquitous data structures

7.6 Summary

● A stack is a powerful tool for parentheses matching, or in general, matching entities that must occur in opening and closing pairs.

● An infix expression may be converted in O(n) (linear) time into the equivalent infix form, using a stack.

● A stack class may be implemented using a vector or a linked list as component, with careful attention being paid to the “end” at which push and pop are performed in order to maintain O(1) time for these operations.