...
GATE 1992
January 26, 2024
TIFR PHD CS & SS 2020
January 26, 2024
GATE 1992
January 26, 2024
TIFR PHD CS & SS 2020
January 26, 2024

Data-Structures

Question 36

Suppose a stack implementation supports, in addition to PUSH and POP, an operation REVERSE, which reverses the order of the elements on the stack.
(a) To implement a queue using the above stack implementation, show how to implement ENQUEUE using a single operation and DEQUEUE using a sequence of 3 operations.
(b) The following postfix expression, containing single digit operands and arithmetic operators + and *, is evaluated using a stack.

 5 2 * 3 4 + 5 2 * * + 

Show the contents of the stack.

    (i) After evaluating 5 2 * 3 4 +
    (ii) After evaluating 5 2 * 3 4 + 5 2
    (iii) At the end of evaluation.
A
Theory Explanation is given below.
Question 36 Explanation: 
(a) Enqueue → push
Dequeue → reverse, pop, reverse
(b) (i) After evaluating 5 2 * 3 4 +
Sol:

7(3+4) 10(5*2)
(ii) After evaluating 5 2 * 3 4 + 5 2
Sol:
25(5*5) 7(3+4) 10(5*2)
(iii) At the end of evaluation
Sol: 80
Correct Answer: A
Question 36 Explanation: 
(a) Enqueue → push
Dequeue → reverse, pop, reverse
(b) (i) After evaluating 5 2 * 3 4 +
Sol:

7(3+4) 10(5*2)
(ii) After evaluating 5 2 * 3 4 + 5 2
Sol:
25(5*5) 7(3+4) 10(5*2)
(iii) At the end of evaluation
Sol: 80

Leave a Reply

Your email address will not be published. Required fields are marked *