Convert infix to postfix and prefix using stack

(A + B) * C + D / (E + F * G) – H

This answer is restricted. Please login to view the answer of this question.

Login Now

Infix to Postfix

Scanned Character Stack Poststack
( (
A ( A
+ (+ A
B (+ AB
) AB+
* * AB+
C * AB+C
+ + AB+C*
D + AB+C*D
/ +/ AB+C*D
( +/( AB+C*D
E +/( AB+C*DE
+ +/(+ AB+C*DE
F +/(+ AB+C*DEF
* +/(+* AB+C*DEF
G +/(+* AB+C*DEFG
) AB+C*DEFG*+/+
AB+C*DEFG*+/+
H AB+C*DEFG*+/+H
AB+C*DEFG*+/+H-

Postfix: AB+C*DEFG*+/+H-

Infix to Prefix:

First we will reverse the infix express and find postfix. Then after we will reverse the result to get the prefix.

Reverse: H-(G+F+E)/D+C*(B+A)

Scanned Character Stack Postfix
H H
H
( -( H
G -( HG
* -(* HG
F -(* HGF
+ -(+ HGF*
E -(+ HGF*E
) HGF*E+
/ -/ HGF*E+
D -/ HGF*E+D
+ -+ HGF*E+D/
C -+ HGF*E+D/C
* -+* HGF*E+D/C
( -+*( HGF*E+D/C
B -+*( HGF*E+D/CB
+ -+*(+ HGF*E+D/CB
A -+*(+ HGF*E+D/CBA
) -+* HGF*E+D/CBA+
HGF*E+D/CBA+*+-

Now, for prefix, we will reverse the postfix

Prefix: -+*+ABC/D+E*FGH

If you found any type of error on the answer then please mention on the comment or report an answer or submit your new answer.
Leave your Answer:

Click here to submit your answer.

Discussion
0 Comments
  Loading . . .