The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
3.5k 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.7k points)
edited by | 3.5k views
+1
The full question is not visible in GO hardcopy book which is printed.Only 823^ is visible and after some tabs /.

Anyone having GO physical copy please validate my claim.

3 Answers

+27 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.6k points)
edited by
+5 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 (7.5k points)
–3 votes
Ans is (A) 6 ,1
answered by Loyal (7.1k points)
Answer:

Related questions



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

44,284 questions
49,776 answers
164,290 comments
65,856 users