Download presentation
Presentation is loading. Please wait.
1
Queue Applications Lecture 31 Tue, Apr 11, 2006
2
Topics Evaluating infix expressions 4/18/2019 Queues
3
Infix Expression Evaluation
An infix expression with one (binary) operator is written in the order: left-operand, operator, right-operand. Example: 4/18/2019 Queues
4
Disadvantages of Infix Notation
Parentheses are often needed to indicate order of operation. Example: (3 + 4) * (5 + 6) Operators follow a precedence hierarchy. Example: * 5 – 6 / 7 Operators have left or right associativity. Example: 100 – 50 – 10 – 5 – 1 4/18/2019 Queues
5
Infix Expression Evaluation
To evaluate an infix expression, we first convert it to postfix. Then 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. 4/18/2019 Queues
6
Infix 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. 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. 4/18/2019 Queues
7
Infix 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. Process the queue as a post-fix expression. 4/18/2019 Queues
8
Example Convert the expression 1 + 2*(3*4 + 5)/6 – 7
from infix to postfix notation. 4/18/2019 Queues
9
Example Read 1 : Enqueue 1. Read + : Push +. Read 2 : Enqueue 2.
Stack Queue 1 Read + : Push +. Stack + Queue 1 Read 2 : Enqueue 2. Stack + Queue 2 1 Read * : Push *. Stack + * Queue 2 1 Read ( : Push (. Stack + * ( Queue 2 1 4/18/2019 Queues
10
Example Read 3 : Enqueue 3. Read * : Push *. Read 4 : Enqueue 4.
Stack + * ( Queue 3 2 1 Read * : Push *. Stack + * ( * Queue 3 2 1 Read 4 : Enqueue 4. Stack + * ( * Queue Read + : Pop and enqueue *, push +. Stack + * ( + Queue * Read 5 : Enqueue 5. Stack + * ( + Queue 5 * 4/18/2019 Queues
11
Example Read ) : Pop and enqueue +, pop (.
Stack + * Queue + 5 * Read / : Pop and enqueue *, push /. Stack + / Queue * + 5 * Read 6 : Enqueue 6. Stack + / Queue 6 * + 5 * Read – : Pop and enqueue / and +, push –. Stack – Queue + / 6 * + 5 * Read 7 : Enqueue 7, pop and enqueue –. Stack Queue – 7 + / 6 * + 5 * 4/18/2019 Queues
12
Example The queue contains – 7 + / 6 * + 5 * 4 3 2 1.
These tokens will be dequeued in the order * 5 + * 6 / + 7 –, which is the postfix order for the original expression. Read 1 : Push 1. Stack 1 Read 2 : Push 2. Stack 2 4/18/2019 Queues
13
Example Read 3 : Push 3. Read 4 : Push 4.
Stack 1 2 3 Read 4 : Push 4. Stack Read * : Pop 4 and 3, push 12. Stack Read 5 : Push 5. Stack Read + : Pop 5 and 12, push 17. Stack 4/18/2019 Queues
14
Example Read * : Pop 17 and 2, push 34. Read 6 : Push 6.
Stack 1 34 Read 6 : Push 6. Stack Read / : Pop 6 and 34, push Stack Read + : Pop and 1, push Stack 6.667 Read 7 : Push 7. Stack 4/18/2019 Queues
15
Example Read – : Pop 7 and 6.667, push -0.333. Stack -0.333. 4/18/2019
Queues
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.