Showing posts with label Stack. Show all posts
Showing posts with label Stack. Show all posts

Thursday 18 August 2016

Conversion of Infix expression to Postfix expression Examples

Conversion of Infix expression to Postfix expression Example 2

Conversion of Infix expression to Postfix expression

Example 2:

Given Infix Expression:         7 + 5 * 3 ^ 2 / ( 9 – 2 ^ 2 ) + 6 * 4



Symbol
Operator Stack
Postfix String
[0]
[1]
[2]
[3]
[4]

1
7






7
2
+
+





7
3
5
+





75
4
*
+
*




75
5
3
+
*




753
6
^
+
*
^



753
7
2
+
*
^



7532
8
/
+
/




7532^*
9
(
+
/
(



7532^*
10
9
+
/
(



7532^*9
11
-
+
/
(
-


7532^*9
12
2
+
/
(
-


7532^*92
13
^
+
/
(
-
^

7532^*92
14
2
+
/
(
-
^

7532^*922
15
)
+
/




7532^*922^-
16
+
+





7532^*922^-/+
17
6
+





7532^*922^-/+6
18
*
+
*




7532^*922^-/+6
19
4
+
*




7532^*922^-/+64
20

Stack empty
7532^*922^-/+64*+


So the postfix expression is  7532^*922^-/+64*+








Conversion of Infix expression to Postfix expression Example 1


Conversion of Infix expression to Postfix expression

Example 1:

Given Infix Expression:         A - ( B / C + ( D % E * F ) / G ) * H



Symbol
Operator Stack
Postfix String
[0]
[1]
[2]
[3]
[4]

1
A






A
2
-
-





A
3
(
-
(




A
4
B
-
(




AB
5
/
-
(
/



AB
6
C
-
(
/



ABC
7
+
-
(
+



ABC/
8
(
-
(
+
(


ABC/
9
D
-
(
+
(


ABC/D
10
%
-
(
+
(
%

ABC/D
11
E
-
(
+
(
%

ABC/DE
12
*
-
(
+
(
*

ABC/DE%
13
F
-
(
+
(
*

ABC/DE%F
14
)
-
(
+



ABC/DE%F*
15
/
-
(
+
/


ABC/DE%F*
16
G
-
(
+
/


ABC/DE%F*G
17
)
-





ABC/DE%F*G/+
18
*
-
*




ABC/DE%F*G/+
19
H
-
*




ABC/DE%F*G/+H
20

Stack Empty
ABC/DE%F*G/+H*-



So Postfix expression is       ABC/DE%F*G/+H*-