Suppose a stack implementation supports, in addition to PUSH and POP, an operation REVERSE, which reverses the order of elements on the stack. To implement a queue using the above stack implementation: A. ENQUEUE can be implemented as a single operation and DEQUEUE as a sequence of 3 operations. B. The following postfix expression containing single digit operands and arithmetic operators + and * is evaluated: 5 2 * 3 4 * 5 2 + - Show the contents of the stack after evaluating: 5 2 * Which of the following correctly describes how ENQUEUE and DEQUEUE are implemented using this stack with REVERSE? A. ENQUEUE = PUSH; DEQUEUE = REVERSE, POP, REVERSE B. ENQUEUE = REVERSE, PUSH, REVERSE; DEQUEUE = POP C. ENQUEUE = PUSH; DEQUEUE = POP D. ENQUEUE = REVERSE; DEQUEUE = POP

GATE 2000 · Programming and Data Structures · Stack · medium

Answer: ENQUEUE = PUSH (1 operation); DEQUEUE = REVERSE, POP, REVERSE (3 operations). Answer: A.

  1. Design ENQUEUE operation: When we enqueue x, it goes to the back of the queue. In stack terms, we simply PUSH x onto the top. The stack bottom represents the queue front (oldest element). This is a single operation.
  2. Design DEQUEUE operation: The oldest element (queue front) is at the bottom of the stack. To remove it: 1. REVERSE: flip the stack — the bottom (oldest) element is now on top 2. POP: remove and return the oldest element (queue front) 3. REVERSE: flip the remaining stack back — order is restored for remaining elements This is a sequence of 3 operations.