Infix to Postfix Example

Infix to Postfix Example - CS310 Fall 2009 Infix to Postfix...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS310 Fall 2009 Infix to Postfix Example Given the expression: 2 * ( 3 + 4 ) + 6 / ( 1 + 2 ) + 3 Token #1: 2 is a number, enqueue it. Token #2: * is an operator, stack is empty, push it. Token #3: ( push the (. Token #4: 3 is a number, enqueue it. Token #5: + is an operator, top of stack is a (, push the +. Token #6: 4 is a number, enqueue it. Token #7: ) pop and enqueue until the ( is reached. Discarding the parenthesis. Token #8: + is an operator. Stack is not empty, top stack token > current token. Pop and enqueue from the stack, then push current token. That is, pop the * off the stack and enqueue it, then push the +. Token #9: 6 is a number, enqueue it. Token #10: / is an operator. The stack is not empty, does not have ( on the top, + is not > / so push the /. Token #11: ( push the (. Token #12: 1 is a number, enqueue it. Token #13: + is an operator. Top of stack is (, push the + Token #14: 2 is a number, enqueue it. Token #15: ) Until ( is reached, pop and enqueue.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Infix to Postfix Example - CS310 Fall 2009 Infix to Postfix...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online