Infix (3 ↑ 2 + 5 * 4 ↑ 3 – 18 / 9) to Postfix Conversion ?


Consider the following infix expression:

3 ↑ 2 + 5 * 4 ↑ 3 – 18 / 9 

a)     Convert it into postfix expression using stack.

Write each step of this conversion using the following table.

Consider the following infix expression:

3 ↑ 2 + 5 * 4 ↑ 3 – 18 / 9

      a)   Convert it into postfix expression using stack.

   Write each step of this conversion using the following table.

Symbol

Postfix

Stack

 

 

 

 

 

 



Solution

Symbol

Stack

Postfix

Steps

3

Empty

3

3 is an operand so append it to the postfix expression.

↑

↑

3

↑ is an operator and the stack is empty, push the ↑ to the stack.

2

↑

3 2

2 is an operand, append it to the postfix expression.

+

+

3 2 ↑

+ is an operator and has lower precedence than the ↑ on the top of the stack, pop ↑ from the stack and add it to the postfix expression.  Push the + to the stack.

5

+

3 2 ↑ 5

5 is an operand, append it to the postfix expression.

*

+ *

3 2 ↑ 5

 * is an operator and has greater precedence than the + on the top of the stack, push * to the top of the stack.

4

+ *

3 2 ↑ 5 4

4 is an operand, append it to the postfix expression.

↑

+ * ↑

3 2 ↑ 5 4

↑ is an operator and has greater precedence than the * on the top of the stack, push ↑ to the top of the stack.

3

+ * ↑

3 2 ↑ 5 4 3

3 is an operand, append it to the postfix expression.

 

 

 


-

+ *

3 2 ↑ 5 4 3 ↑

- is an operator and has lower precedence than the ↑ on the top of the stack, pop ↑ from the stack and append it to the postfix expression.

+

3 2 ↑ 5 4 3 ↑ *

- is an operator and has lower precedence than the * on the top of the stack, pop * from the stack and append it to the postfix expression.

-

3 2 ↑ 5 4 3 ↑ * +

- is an operator, is left-to-right associative, and has the same precedence as the + at the top of the stack, pop + from the stack and append it to the postfix expression. Also push the - to the stack.

18

-

3 2 ↑ 5 4 3 ↑ * + 18

18 is an operand, append it to the postfix expression.

/

- /

3 2 ↑ 5 4 3 ↑ * + 18

/ is an operator and has greater precedence than the - on the top of the stack, push / to the top of the stack.

9

- /

3 2 ↑ 5 4 3 ↑ * + 18 9

9 is an operand, append it to the postfix expression.

 

No more

Inputs

-

3 2 ↑ 5 4 3 ↑ * + 18 9 /

Pop / from the stack and append it to the postfix expression.

 

Empty

3 2 ↑ 5 4 3 ↑ * + 18 9 / -

Pop - from the stack and append it to the postfix expression.

 

Postfix Expression is:  3 2 ↑ 5 4 3 ↑ * + 18 9 / -

 

Comments