The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
3.1k views

The following postfix expression with single digit operands is evaluated using a stack:

$$8 \ 2 \ 3 \ ^\hat{} /  \ 2 \ 3 * + 5 \ 1 * -$$

Note that $^\hat{}$ is the exponentiation operator. The top two elements of the stack after the first $*$ is evaluated are

  1. $6, 1$
  2. $5, 7$
  3. $3, 2$
  4. $1, 5$
asked in DS by Veteran (59.4k points)
edited by | 3.1k views

3 Answers

+26 votes
Best answer

push $8$  so stack is $8$

push $2$ so stack is $8 \ 2$

push $8 \ 2 \ 3$

$^\hat{}$ pop $3$ and $2$ perform opn $2 ^\hat{} 3$ and push to stack. stack is $8 \ 8$

/ pop $8$ and $8$ perform $8/8$ and push result to stack . stack is $1$

push $2$ stack is $1 \ 2$

push $3$ stack is $1 \ 2 \ 3$

$*$ pop $3$ and $2$ perform by $2*3$ and push . stack is $1 \ 6$

 
hence answer is A

answered by Boss (11.4k points)
edited by
+4 votes
Operation   Expression   Stack 
   823 ^ /23 * 51 * -   Empty 
Push  23 ^ /23 * 51 * -  8
Push  3 ^ /23 * 51 * -  8 2
Push  ^ /23 * 51 * -  8 2 3
Pop  /23 * 51 * -  8 (2^3)
Pop  23 * 51 * -  8/8
Push   3 * 51 * -  1 2
Push  * 51 * -  1 2 3
Pop  51 * -  1 (2*3)

So top two elements after 1st * is 6 , 1

answered by Loyal (6.4k points)
–3 votes
Ans is (A) 6 ,1
answered by Loyal (6.2k points)


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

35,526 questions
42,803 answers
121,617 comments
42,166 users