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 Postfix
A A
A
B AB
AB
C ABC
* –* ABC
/ –*( ABC
D –*( ABCD
+ –*(+ ABCD
E –*(+ ABCDE
/ –*(+/ ABCDE
F –*(+/ ABCDEF
–*(+/- ABCDEF/
G –*(+/- ABCDEF/G
) –* ABCDEF/G+-
ABCDEF/G+-*
H ABCDEF/G+-*H
ABCDEF/G+-*H—

Postfix: ABCDEF/G+-*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+D)*C-B-A

Scanned Character Stack Postfix
H H
H
( -( H
G -( HG
-(- HG
F -(- HGF
/ -(-/ HGF
E -(-/ HGFE
+ -(-+ HGFE/
D -(-+ HGFE/D
) HGFE/D+-
* -* HGFE/D+-
C HGFE/D+-C
– – HGFE/D+-C*
B – – HGFE/D+-C*B
– – – HGFE/D+-C*B
A – – – HGFE/D+-C*BA
HGFE/D+-C*BA—

Now for prefix we reverse the postfix:

Prefix:  —AB*C-+D/EFGH

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 . . .