Suppose a stack implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Which one of the following statements is TRUE with respect to this modified stack?

Suppose a stack implementation supports an instruction REVERSE, which reverses the order of elements on the stack, in addition to the PUSH and POP instructions. Which one of the following statements is TRUE with respect to this modified stack? Correct Answer A queue can be implemented where ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.

Explanation:

We want to implement queue using stack which has three functions reverse, push and pop.

Example:

Suppose we want to insert a,b in queue using stack then

ENQUEUE item a:

Step 1: reverse (stack)

Step 2: push (a)

Step 3: reverse(stack)

[ alt="F2 Raju Madhu 17.07.20 D4" src="//storage.googleapis.com/tb-img/production/20/07/F2_Raju_Madhu_17.07.20_D4.png" style="width: 232px; height: 104px;">

 

ENQUEUE item b:

Step 1: reverse (stack)

Step 2: push (b)

Step 3: reverse(stack)

[ alt="F2 Raju Madhu 17.07.20 D5" src="//storage.googleapis.com/tb-img/production/20/07/F2_Raju_Madhu_17.07.20_D5.png" style="width: 222px; height: 106px;">

  • After inserting b to stack if we want to DEQUEUE then only pop of top element is sufficient.
  • So for ENQUEUE takes a sequence of three instructions and DEQUEUE takes a single instruction.
  • Similarly DEQUEUE (reverse pop reverse) and ENQUEUE ( push ) is also valid for queue implementation.  


Hence option 3 is the correct answer.

Related Questions