H-SC COMS 262 - Lecture 27 - Queue Applications

Unformatted text preview:

Queue ApplicationsInfix Expression EvaluationDisadvantages of Infix NotationSlide 4Slide 5Slide 6Slide 7Example((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Slide 50Slide 51Slide 52InfixToPostfix.cppQueue ApplicationsLecture 27Thu, Mar 27, 200801/14/19Queues 2Infix Expression EvaluationAn infix expression with one (binary) operator is written in the order: left-operand, operator, right-operand.Example: 3 + 401/14/19Queues 3Disadvantages of Infix NotationParentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6)Operators at different precedence levels follow a precedence hierarchy.Example: 3 + 4 * 5 – 6 / 7Operators at the same precedence level have a left or right associativity.Example: 100 – 50 – 10 – 5 – 101/14/19Queues 4Infix Expression EvaluationTo evaluate an infix expression, we first convert it to postfix.Begin with an empty stack and an empty queue. Process the tokens from left to right according to the following rules. If the token is a number, Enqueue the token. If the token is a left parenthesis, Push the token onto the stack.01/14/19Queues 5Infix Expression EvaluationIf the token is a right parenthesis, Pop tokens off the stack and enqueue them until a left parenthesis is popped. Discard the right and left parentheses.01/14/19Queues 6Infix Expression EvaluationIf the token is an operator, Pop tokens off the stack and enqueue them until An operator of lower precedence is on top of the stack, or A left parenthesis is on top of the stack, or The stack is empty. Push the operator onto the stack.01/14/19Queues 7Infix Expression EvaluationAfter processing the last token Pop all tokens off the stack and enqueue them. The queue now contains the expression in post-fix notation. Now process the queue as a post-fix expression.01/14/19Queues 8ExampleConvert the expression((1 + 2)*3 – 4/(5 + 6 – 7*8))*9from infix to postfix notation.01/14/19Queues 9((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue01/14/19Queues 10((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (01/14/19Queues 11((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (01/14/19Queues 12((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 101/14/19Queues 13((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 101/14/19Queues 14((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 12 ( ( + 1 201/14/19Queues 15((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 12 ( ( + 1 2) ( 1 2 +01/14/19Queues 16((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 12 ( ( + 1 2) ( 1 2 +* ( * 1 2 +01/14/19Queues 17((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 12 ( ( + 1 2) ( 1 2 +* ( * 1 2 +3 ( * 1 2 + 301/14/19Queues 18((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue( (( ( (1 ( ( 1+ ( ( + 12 ( ( + 1 2) ( 1 2 +* ( * 1 2 +3 ( * 1 2 + 3– ( – 1 2 + 3 *01/14/19Queues 19((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 401/14/19Queues 20((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 401/14/19Queues 21((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 401/14/19Queues 22((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 501/14/19Queues 23((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 5+ ( – / ( + 1 2 + 3 * 4 501/14/19Queues 24((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 5+ ( – / ( + 1 2 + 3 * 4 56 ( – / ( + 1 2 + 3 * 4 5 601/14/19Queues 25((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 5+ ( – / ( + 1 2 + 3 * 4 56 ( – / ( + 1 2 + 3 * 4 5 6– ( – / ( – 1 2 + 3 * 4 5 6 +01/14/19Queues 26((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 5+ ( – / ( + 1 2 + 3 * 4 56 ( – / ( + 1 2 + 3 * 4 5 6– ( – / ( – 1 2 + 3 * 4 5 6 +7 ( – / ( – 1 2 + 3 * 4 5 6 + 701/14/19Queues 27((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue4 ( – 1 2 + 3 * 4/ ( – / 1 2 + 3 * 4( ( – / ( 1 2 + 3 * 45 ( – / ( 1 2 + 3 * 4 5+ ( – / ( + 1 2 + 3 * 4 56 ( – / ( + 1 2 + 3 * 4 5 6– ( – / ( – 1 2 + 3 * 4 5 6 +7 ( – / ( – 1 2 + 3 * 4 5 6 + 7* ( – / ( – * 1 2 + 3 * 4 5 6 + 701/14/19Queues 28((1 + 2)*3 – 4/(5 + 6 – 7*8))*9Input token Stack Queue8 ( – / ( – * 1 2 + 3 …


View Full Document

H-SC COMS 262 - Lecture 27 - Queue Applications

Download Lecture 27 - Queue Applications
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 27 - Queue Applications and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 27 - Queue Applications 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?