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*-
No comments:
Post a Comment