Stacks & Their Applications COP 3502. Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before.

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

Stack & Queues COP 3502.
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
More on Stacks and Queues. As we mentioned before, two common introductory Abstract Data Type (ADT) that worth studying are Stack and Queue Many problems.
COMPSCI 105 S Principles of Computer Science 13 Stacks.
C o n f i d e n t i a l Developed By Nitendra NextHome Subject Name: Data Structure Using C Title : Overview of Stack.
CS 206 Introduction to Computer Science II 03 / 04 / 2009 Instructor: Michael Eckmann.
Chapter 6: Stacks STACK APPLICATIONS STACK IMPLEMENTATIONS CS
Stacks CS 3358 – Data Structures. What is a stack? It is an ordered group of homogeneous items of elements. Elements are added to and removed from the.
Topic 15 Implementing and Using Stacks
E.G.M. Petrakisstacks, queues1 Stacks  Stack: restricted variant of list  elements may by inserted or deleted from only one end : LIFO lists  top: the.
Introduction to Stacks What is a Stack Stack implementation using arrays. Application of Stack.
Stacks.
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.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
Introduction to Stacks What is a Stack Stack implementation using array. Stack implementation using linked list. Applications of Stack.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
Fall 2007CS 2251 Stacks Chapter 5. Fall 2007CS 2252 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop,
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
CST Razdan et al Razdan with contribution from others 1 Chapter 6 Stacks Anshuman Razdan Div of Computing.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
1 Introduction to Stacks What is a Stack? Stack implementation using array. Stack implementation using linked list. Applications of Stacks.
Topic 15 Implementing and Using Stacks
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures Stacks.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
Stack Applications.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
Chapter 4 Stacks Stacks A stack is a linear data structure that can be accessed only at one of its ends for storing and retrieving. Its called.
Computer Science Department Data Structure & Algorithms Problem Solving with Stack.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Stacks  Introduction  Applications  Implementations  Complex Applications.
CHAPTER 3 STACK CSEB324 DATA STRUCTURES & ALGORITHM.
Data Structures & Algorithms
CHP-3 STACKS.
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.
Prof. I. J. Chung Data Structure #5 Professor I. J. Chung.
Applications of Stack Maitrayee Mukerji. Stacks Last In First Out (LIFO List) ◦ FILO? Insertions and Deletions from the same end called the Top Push(),
Review Use of Stack Introduction Stack in our life Stack Operations
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.
CS Data Structures Chapter 6 Stacks Mehmet H Gunes
Stacks Chapter 6.
Infix to postfix conversion
MEMORY REPRESENTATION OF STACKS
Objectives In this lesson, you will learn to: Define stacks
CSC 172 DATA STRUCTURES.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Stacks Chapter 4.
STACK CHAPTER 03 Developed By :- Misha Ann Alexander Data Structures.
Visit for more Learning Resources
COMPUTER 2430 Object Oriented Programming and Data Structures I
Stacks Chapter 5 Adapted from Pearson Education, Inc.
More About Stacks: Stack Applications
Stacks.
COMPUTER 2430 Object Oriented Programming and Data Structures I
Cs212: Data Structures Computer Science Department Lecture 6: Stacks.
Stacks.
Queue Applications Lecture 31 Mon, Apr 9, 2007.
Stacks.
Topic 15 Implementing and Using Stacks
Stack.
Queue Applications Lecture 31 Tue, Apr 11, 2006.
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.
Presentation transcript:

Stacks & Their Applications COP 3502

Stacks  A stack is a data structure that stores information arranged like a stack.  We have seen stacks before when we used a stack to trace through recursive programs.  The essential idea is that the last item placed into the stack is the first item removed from the stack  Or LIFO(Last In, First Out) for short TOP

Stacks  There are two operations that modify the contents of a stack:  Push – inserts some data onto the stack.  Pop – that extracts the top-most element from the stack. PUSHPOP TOP

Abstract Data Type  Side Note: What is an Abstract Data Type (in C)?  An abstract data type is something that is not built into the language.  So int and double are built-in types in the C language.  An Abstract Data Type is something we “build”  and is often defined in terms of its behavior  Definition from Wikipedia:  An abstract data type is defined indirectly, only by the operations that may be performed on it (i.e. behavior).  So example of ADT’s we’ve seen so far are:  Linked Lists, Doubly Linked Lists, Circularly Linked Lists  And now Stacks

Stacks  Stacks:  Stacks are an Abstract Data Type  They are NOT built into C  So we must define them and their behaviors  Stack behavior:  A data structure that stores information in the form of a stack.  Contains any number of elements of the same type.  Access policy:  The last item placed on the stack is the first item removed from the stack.

Stacks  Stacks:  Basic Operations: PUSH and POP  PUSH – PUSH an item on the top of the stack. PUSH TOP TOP (Stack before Push Operation)(Stack after Push Operation)

Stacks  Stacks:  Basic Operations: PUSH and POP  POP – POP off the item in the stack and return it. POP TOP TOP (Stack after Pop Operation)(Stack before Pop Operation) 19 Element removed:

Stacks  Stacks:  Other useful operations:  empty – typically implemented as a boolean function that returns true if no items are in the stack.  full – returns true if no more items can be added to the stack. – In theory a stack should never be full, but any actual implementation has a limit on the # of elements it can store.  top – simply returns the value stored at the top of the stack without popping it off the stack.

Stacks  Simple Example:  PUSH(7)  PUSH(3)  PUSH(2)  POP  PUSH(5)  POP

Stacks  Stack Applications:  Whenever we need a LIFO component to a system.  There are several examples outside the scope of this class you will see in CS2  For now, we’ll go over 2 classical examples  Converting infix expressions to a postfix expression.  Evaluating a postfix expression.

Stacks  An Infix Arithmetic Expression:  Consider the expression: 2 * * 4  We know how to evaluate this expression because we usually see it in this form.  Multiply 2 * 3 = 6, store this in your head.  Add 5, now store 11 in your head.  Now multiply 3 * 4 = 12.  Retrieve 11 and add to 12.  This was easy because we know the rules of precedence for infix expressions.  But what if we didn’t know these rules?

Stacks  There are other ways of writing this expression:  2 3 * * +  is the Postfix form of:  2 * * 4  And if you read from left to right, the operators are always in the correct evaluation order.

Stacks  So there are 3 types of notations for expressions  Infix(A + B)  Postfix(A B +)  Prefix(+ A B)  We’re just going to worry about Infix and Postfix.  What does this have to do with Stacks??

Stacks  We are going to use stacks to: 1)Evaluate postfix expressions 2)Convert infix expressions to a postfix expressions

Stacks  Evaluating a Postfix Expression (A B +)  Consider the Postfix expression:  2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP, – you POP off the last two values off the stack, s1 and s2 respectively. – Then you PUSH value s2 OP s1 back onto the stack. » (If there were not two values to pop off, the expression is not in valid Postfix notation) 3)When you are done, you should have a single value left on the stack that the expression evaluates to.

2 3 * * +  The Rules are: 1)Each number gets PUSHed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. PUSH(2) 2

2 3 * * +  The Rules are: 1)Each number gets PUSHed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. PUSH(3) 2 3

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. POP s1 = POP s2 = 2 PUSH(2 * 3 = 6)

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. PUSH(5) 6 5

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. POP s1 = POP s2 = 6 PUSH(6 + 5 = 11)

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. PUSH(3) 3 11

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. PUSH(4)

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. POP s1 = POP s2 = 3 PUSH(3 * 4 = 12)

2 3 * * +  The Rules are: 1)Each number gets pushed onto the stack. 2)Whenever you get to an operator OP,  you POP off the last two values off the stack, s1 and s2 respectively.  Then you PUSH value s2 OP s1 back onto the stack. – (If there aren’t 2 values to pop, the expression is not valid Postfix) 3)When you are done, you should have a single value left on the stack that the expression evaluates to. POP s1 = POP s2 = 11 PUSH( = 23)

Stacks  So now we know how to evaluate a Postfix expression using a stack.  NOW, we’re going to convert an Infix expression to Postfix using a stack.  Before we get started, we need to know operator precedence: 1)(In the expression, left paren has highest priority 2)* / Going from left to right whichever comes 1 st 3)+ - Going from left to right whichever comes 1 st 4)(In the stack, the left parentheses has lowest priority

Stacks  Converting an Infix expression to a Postfix expression.  Rules: 1)For all operands, automatically place them in the output expression. 2)For an operator (+, -, *, /, or a parenthesis)  IF the operator is an open parenthesis, push it onto the stack.  ELSE IF the operator is an arithmetic one, then do this: – Continue popping off items off the stack and placing them in the output expression until you hit an operator with lower precedence than the current operator or until you hit an open parenthesis. – At this point, push the current operator onto the stack.  ELSE Pop off all operators off the stack one by one, placing them in the output expression until you hit thefirst(matching) open parenthesis. When this occurs, pop off the open parenthesis and discard both ()s.

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) PUSH ( ( Output: STACK

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: STACK Output: 7

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: 7 STACK PUSH * *

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: 7 STACK PUSH ( * (

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: 7 STACK * ( Output: 7 6

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: 7 6 STACK * ( PUSH + +

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: 7 6 STACK * ( + Output: 7 6 3

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: STACK * ( + POP + Output: POP (

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: STACK * POP * Output: * PUSH + +

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: STACK Output: * PUSH ( + (

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * STACK Output: * 2 + (

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * STACK Output: * 2 + ( PUSH – –

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * 2 STACK Output: * ( –

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * 2 3 STACK Output: * 2 3 – + ( – POP – POP (

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * 2 3 – STACK Output: * 2 3 – + + POP + PUSH +

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * 2 3 – + STACK Output: * 2 3 – + 1 +

 Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s. ( 7 * ( ) + ( 2 – 3 ) + 1 ) ( Output: * 2 3 – + 1 STACK Output: * 2 3 – POP + POP (

Converting Infix to Postfix  Given the Infix expression:  ( 7 * ( ) + ( 2 – 3 ) + 1 )  Our final Postfix expression was this:  * 2 3 –  We can check if this is correct by evaluating the Postfix expression like we did before.  Let’s do that on the board…  We should get ( 7 * ( ) + ( 2 – 3 ) + 1 ) =  62

Infix to Postfix  Try this example:  5 * * 4  Don’t forget to check your answer!  Rules: 1)For all operands, automatically put in output expression. 2)For an operator +, -, *, /, or (, )  IF the operator is an open paren, PUSH it.  ELSE IF the operator is an arithmetic one, then do this: – Continue POPing items and placing them in the output until you hit an OP with < precedence than the curr OP or until you hit an open paren. – At this point, PUSH the curr OP.  ELSE POP off all OPs off the stack 1 by 1, placing them in the output expression until you hit the 1 st ) open paren. When this occurs, POP off the open paren and discard both ()s.