Prefix-Postfix-Expression
Question 1 |
The postfix for a string is ABC + -D*, the actual string will be
(A-(B+C))*D | |
((A-B)+C)*D | |
((A+B)-C)*D
| |
(A+(B-C))*D |
Question 2 |
The postfix expression for the infix expression A+B+(C+D)/F+D+E is
AB+CD+ *F/D+E* | |
ABCD+ *F/+ DE* + | |
A*B+CD/F+DE++ | |
None of these
|
Question 2 Explanation:
The given infix expression is,
(A + B + (C + D))/ (F + D + F)
⇒ Push ‘(’
⇒ Read A
⇒ Push ‘+’
⇒ Read B
⇒ Since ‘+’ is Left associative, so ‘+’ in stack will have higher procedure then the current ‘+’, hence pop it,
⇒ Push ‘+’
⇒ Push ‘(’
⇒ Read C
⇒ Push ‘+’
⇒ Read D,
⇒ Now on looking ‘)’ we will pop all the operators till first ‘(‘,
⇒ Pop
⇒ Push /
⇒ Push ‘(’
⇒ Read F,
⇒ Push ‘+’
⇒ Read D,
⇒ Since + is left associative, so pop ‘+’,
⇒ Push up
⇒ Read E,
⇒ Since ‘(’ came so pop till ‘)’,
⇒ Pop ‘/’
Hence, postfix expression is,
AB + CD ++ FD ++ E +/
(A + B + (C + D))/ (F + D + F)
⇒ Push ‘(’

⇒ Read A

⇒ Push ‘+’

⇒ Read B

⇒ Since ‘+’ is Left associative, so ‘+’ in stack will have higher procedure then the current ‘+’, hence pop it,

⇒ Push ‘+’

⇒ Push ‘(’

⇒ Read C

⇒ Push ‘+’

⇒ Read D,

⇒ Now on looking ‘)’ we will pop all the operators till first ‘(‘,

⇒ Pop

⇒ Push /

⇒ Push ‘(’

⇒ Read F,

⇒ Push ‘+’

⇒ Read D,

⇒ Since + is left associative, so pop ‘+’,

⇒ Push up

⇒ Read E,

⇒ Since ‘(’ came so pop till ‘)’,

⇒ Pop ‘/’

Hence, postfix expression is,
AB + CD ++ FD ++ E +/
Question 3 |
The value of the postfix expression abc*de+/- where a=20, b=3, c=4, d=1 and e=5 is
134 | |
-22 | |
48 | |
18 |
Question 3 Explanation:
Here we will start traversing the expression from left to right and if an operand is encountered then it will be pushed on the top of stack and if an operator is encountered then top two elements will be popped off from the top of stack and current operator is applied on those two operands and then in the end the result of operation will be pushed back on the top of stack. This procedure continues till the whole expression is traversed.


Step: 7
Hence option 4 is the correct answer.


Step: 7

Hence option 4 is the correct answer.
Question 4 |
The prefix equivalent of the following infix expression is:
a / b - c + d * e - a * c
- + - / a b c * d e * a c | |
+ - - / a b c * d e * a c | |
- + / a b c - * d e * a c | |
- - + / a b c * d e * a c |
Question 4 Explanation:
a / b - c + d * e - a * c
* Start scanning the expression from RHS. * Push the operators on the top of the stack by ensuring that only high priority operator can be pushed over a low priority operator.
* If a low priority operator is encountered in expression while scanning it, and if top of stack have high priority operator then pop it and after that push low priority operator on top of stack.
* Print each operand just after scanning it.





* Start scanning the expression from RHS. * Push the operators on the top of the stack by ensuring that only high priority operator can be pushed over a low priority operator.
* If a low priority operator is encountered in expression while scanning it, and if top of stack have high priority operator then pop it and after that push low priority operator on top of stack.
* Print each operand just after scanning it.





There are 4 questions to complete.