Stacks – Calculator Application

Slides:



Advertisements
Similar presentations
INFIX, PREFIX, & POSTFIX EXPRESSIONS. Infix Notation We usually write algebraic expressions like this: a + b This is called infix notation, because the.
Advertisements

Stacks & Their Applications COP Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.
Stacks - 3 Nour El-Kadri CSI Evaluating arithmetic expressions Stack-based algorithms are used for syntactical analysis (parsing). For example.
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
COSC 2006 Chapter 7 Stacks III
Joseph Lindo Abstract Data Types Sir Joseph Lindo University of the Cordilleras.
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Arithmetic Expressions
Topic 15 Implementing and Using Stacks
Department of Technical Education Andhra Pradesh
Stacks21 Stacks II Adventures in Notation. stacks22 The trouble with infix... Rules for expression evaluation seem simple -- evaluate expression left.
Stacks & Queues Infix Calculator CSC 172 SPRING 2002 LECTURE 5.
Infix to postfix conversion Process the tokens from a vector infixVect of tokens (strings) of an infix expression one by one When the token is an operand.
Infix, Postfix, Prefix.
Reverse Polish Expressions Some general observations about what they are and how they relate to infix expressions. These 9 slides provide details about.
Postfix notation. About postfix notation Postfix, or Reverse Polish Notation (RPN) is an alternative to the way we usually write arithmetic expressions.
1 CSCD 326 Data Structures I Infix Expressions. 2 Infix Expressions Binary operators appear between operands: W - X / Y - Z Order of evaluation is determined.
Topic 15 Implementing and Using Stacks
Evaluation of Expressions
Objectives of these slides:
Exam 1 –Monday June 25 th –open Book / Open Notes –No Electronic Devices (calculators, laptops, etc) –Room Number: W –Time: 5:30pm to 8:00pm.
Data Structures Lecture : Stacks (Infix, Postfix and Prefix Expressions) Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comp 245 Data Structures Stacks. What is a Stack? A LIFO (last in, first out) structure Access (storage or retrieval) may only take place at the TOP NO.
Stack Applications.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
(c) University of Washington20d-1 CSC 143 Java Applications of Trees.
Abstract Data Types and Stacks CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
DATA STRUCTURE & ALGORITHMS CHAPTER 3: STACKS. 2 Objectives In this chapter, you will: Learn about stacks Examine various stack operations Discover stack.
Due: 2007/11/12. Problem 1 Rewrite function Push and Pop (Program 3.10 and 3.12) using an additional variable lastOp as discussed on Page 146. The queue.
Infix to postfix conversion Scan the Infix expression left to right If the character x is an operand  Output the character into the Postfix Expression.
EENG212 Algorithms and Data Structures
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 18: Stacks and Queues (part 2)
Basic Data Structures Stacks. A collection of objects Objects can be inserted into or removed from the collection at one end (top) First-in-last-out.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving data LIFO (Last In First Out) structure.
CC 215 DATA STRUCTURES MORE ABOUT STACK APPLICATIONS Dr. Manal Helal - Fall 2014 Lecture 6 AASTMT Engineering and Technology College 1.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Stacks Access is allowed only at one point of the structure, normally termed the top of the stack access to the most recently added item only Operations.
Stacks Chapter 6.
Revised based on textbook author’s notes.
COMPSCI 107 Computer Science Fundamentals
Infix to postfix conversion
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stack application: postponing data usage
Stacks – Calculator Application
Stacks – Calculator Application
PART II STACK APPLICATIONS
106 Data Structure Homework 1
Stack Data Structure, Reverse Polish Notation, Homework 7
Stacks Chapter 5 Adapted from Pearson Education, Inc.
Stacks, Queues, and Deques
More About Stacks: Stack Applications
Infix to Postfix Conversion
Stacks – Calculator Application
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Infix to Postfix Conversion
Topic 15 Implementing and Using Stacks
CSC 143 Java Applications of Trees.
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
Applications of Stacks and Queues
More About Stacks: Stack Applications
17CS1102 DATA STRUCTURES © 2016 KL University – The contents of this presentation are an intellectual and copyrighted property of KL University. ALL RIGHTS.
Chapter 7 (continued) © 2011 Pearson Addison-Wesley. All rights reserved.
Stacks A stack is an ordered set of elements, for which only the last element placed into the stack is accessible. The stack data type is also known as.
Presentation transcript:

Stacks – Calculator Application

Stacks and Calculators Consider this expression: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) This calculation involves saving intermediate results. First we calculate ( 9 + 8 ). We save 17 (push it to a stack). Then we calculate ( 4 * 6 ) and save 24 (push to a stack). Then we pop 17 and 24, multiply them, save the result. And so on…

Infix and Postfix Notation The standard notation we use for writing mathematical expressions is called infix notation. Why? Because the operators are between the operands. There are two alternative notations: prefix notation: the operator comes before the operands. postfix notation: the operator comes after the operands. Example: infix: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) prefix: (* 5 (+ (* (+ 9 8) (* 4 6)) 7)) postfix: 5 9 8 + 4 6 * * 7 + *

Postfix Notation Example: infix: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) postfix: 5 9 8 + 4 6 * * 7 + * Postfix notation does not need any parentheses. It is pretty easy to write code to evaluate postfix expressions (we will).

Processing a Symbolic Expression How do we process an expression such as: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) postfix: 5 9 8 + 4 6 * * 7 + * One approach is textbook Program 4.2 (page 140). Only few lines of code, but dense and hard to read. Second approach: think of the input as a stream of tokens. A token is a logical unit of input, such as: A number An operator A parenthesis.

Tokens A token is a logical unit of input, such as: A number An operator A parenthesis. What are the tokens in: 51 * (((195 + 8 ) * (4 - 6)) + 7) Answer: 51, *, (, (, (, 195, +, 8, ), *, (, 4, -, 6, ), ), +, 7, ) 19 tokens. Note that a token is NOT a character. For example 195 is one token, but it contains 3 charatcters. We will not discuss how to build tokens from characters. the numbers are the only difficulty

Processing Postfix: Pseudocode input: a list tokens in infix order. output: the result of the calculation (a number). Assumption: the list of tokens can be provided as input (we do not ‘build’ tokens) while(token list is not empty) T = remove next token (number or operator) from list If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = ???

Processing Postfix: Pseudocode input: a list tokens in infix order. output: the result of the calculation (a number). Assumption: the list of tokens can be provided as input (we do not ‘build’ tokens) while(token list is not empty) T = remove next token (number or operator) from list If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack)

Example: Postfix Notation Evaluation Example: we will see how to use this pseudocode to evaluate this postfix expression: 5 9 8 + 4 6 * * 7 + * In infix, the equivalent expression is: 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 )

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 5 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 9 9 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 8 8 9 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = + A = 8 B = 9 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = + A = 8 B = 9 C = 17 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 4 4 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 6 6 4 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 6 B = 4 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 6 B = 4 C = 24 24 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 24 B = 17 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 24 B = 17 C = 408 408 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = 7 7 408 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = + A = 7 B = 408 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = + A = 7 B = 408 C = 415 415 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 415 B = 5

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 415 B = 5 C = 2075 2075

Example: Postfix Notation Evaluation Input: 5 9 8 + 4 6 * * 7 + * while(input remains to be processed) T = next token If T is a number, push(stack, T). If T is an operator: A = pop(stack) B = pop(stack) C = apply operator T on A and B. push(stack, C) final_result = pop(stack) T = * A = 415 B = 5 C = 2075 final_result = 2075

Converting Infix to Postfix Another example of using stacks is converting infix notation to postfix notation. We already saw how to evaluate postfix expressions. By converting infix to postfix, we will be able to evaluate infix expressions as well. input: a stream of tokens in infix order. output: a list of tokens in postfix order.

Converting Infix to Postfix input: a stream of tokens in infix order. Assumption 1: the input is fully parenthesized. That is, every operation (that contains an operator and its two operands) is enclosed in parentheses. 3 + 5 NOT ALLOWED. (3 + 5) ALLOWED. Assumption 2: Each operator has two operands. (2 + 4 + 5) NOT ALLOWED. (2 + (4 + 5)) ALLOWED. Writing code that does not need these assumptions is great (but optional) practice for you. output: a list of tokens in postfix order.

Converting Infix to Postfix input: a stream of tokens in infix order. output: a list of tokens in postfix order. while(the input stream is not empty) T = next token If T is left parenthesis, ignore. If T is a number, insertAtEnd(result, T) If T is an operator, push(op_stack, T). If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op)

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = op_stack = [ ] empty stack result = [ ] (empty list)

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ( op_stack = [ ] empty stack result = [ ] (empty list)

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = 5 op_stack = [ ] empty stack result = [ 5 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = * op_stack = [ * ] result = [ 5 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ( op_stack = [ * ] result = [ 5 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ( op_stack = [ * ] result = [ 5 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ( op_stack = [ * ] result = [ 5 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = 9 op_stack = [ * ] result = [ 5 9 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = + op_stack = [ * + ] result = [ 5 9 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = 8 op_stack = [ * + ] result = [ 5 9 8 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ * ] result = [ 5 9 8 + ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = * op_stack = [ * * ] result = [ 5 9 8 + ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ( op_stack = [ * * ] result = [ 5 9 8 + ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = 4 op_stack = [ * * ] result = [ 5 9 8 + 4 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = * op_stack = [ * * * ] result = [ 5 9 8 + 4 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ * * * ] result = [ 5 9 8 + 4 6 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ * * ] result = [ 5 9 8 + 4 6 * ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ * ] result = [ 5 9 8 + 4 6 * * ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = + op_stack = [ * + ] result = [ 5 9 8 + 4 6 * * ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = 7 op_stack = [ * + ] result = [ 5 9 8 + 4 6 * * 7 ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ * ] result = [ 5 9 8 + 4 6 * * 7 + ]

Example: Infix to Postfix Input: ( 5 * ( ( ( 9 + 8 ) * ( 4 * 6 ) ) + 7 ) ) while(input stream not empty) T = next token If T is left parenthesis, ignore. If T is right parenthesis: op = pop(op_stack) insertAtEnd(result, op) If T is an operator: push(op_stack, T). If T is a number: insertAtEnd(result, T) T = ) op_stack = [ ] result = [ 5 9 8 + 4 6 * * 7 + * ]