edited by
24,780 views
54 votes
54 votes

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

  1. A queue cannot be implemented using this stack.
  2. A queue can be implemented where $\text{ENQUEUE}$ takes a single instruction and $\text{DEQUEUE}$ takes a sequence of two instructions.
  3. A queue can be implemented where $\text{ENQUEUE}$ takes a sequence of three instructions and $\text{DEQUEUE}$ takes a single instruction.
  4. A queue can be implemented where both $\text{ENQUEUE}$ and $\text{DEQUEUE}$ take a single instruction each.
edited by

9 Answers

2 votes
2 votes
To DEQUEUE an item, simply POP. To ENQUEUE an item, we can do following 3 operations 1) REVERSE 2) PUSH 3) REVERSE
1 votes
1 votes

Answer is C. Though we can also implement queue by single operation of ENQUEUE and DEQUEUE using a sequence of 3 operations . ENQUEUE using simple PUSH operation and DEQUEUE(REV,POP,REV)

1 votes
1 votes
Option A is not right bcs we can implement a Queue and we will see how.

Option D is not feasible, as implementing a queue with one stack is a lot of work and cannot be done with a single operation.

Option B is tempting but think about it, suppose you have "1" in stack, now you push "2", so your queue is [1[2[... now you want to dequeue so you reverse the stack and pop 1 and then again reverse the stack, these are three operations, not two.

While Option C is right, see this - say you have "1", you still reverse the whole stack as mandatory procedure then push "2" then you reverse the whole stack again, so now you have [2[1... now you pop 1 in a single operation and that's a perfect queue. To insert another number, you again reverse it then push the number then again reverse it.

Three operations to insert, one to delete.

Option C is the right one.
0 votes
0 votes

 

FOR ENQUEUE: 3 OPERATIONS (REVERSE,PUSH ,REVERSE

Suppose the Order of queue operations is,

Enqueue(3,4,5),Dequeue(),Enqueue(6)

ENQUEUE USING STACK:

 
 
 

3

Then from 2nd Enqueue : Do reverse,PUSH,reverse

  1. Reverse→ Stack will be as it is
  2. Push  
     
     
    4
    3

     

  3. Reverse

     
     
    3
    4

So this is how a Single Enqueue operation would be.

Continue same for all enqueues till Enqueue(5)

Now for DEQUEUE: 1 OPERATION POP

As FIFO order should be followed Our Queue Automatically follows it by Enqueue operation.

So for Dequeue Simply do POP only and 3 will be popped off.

Continue Popping for all Dequeues

SO FOR ENQUEUE 3 OPERATIONS(REVERSE,PUSH,REVERSE) AND FOR DEQUEUE 1 OPERATION(POP)

edited by
Answer:

Related questions

21 votes
21 votes
1 answer
1
go_editor asked Sep 28, 2014
5,566 views
A priority queue is implemented as a Max-Heap. Initially, it has $5$ elements. The level-order traversal of the heap is: $10, 8, 5, 3, 2$. Two new elements $1$ and $7$ ar...
25 votes
25 votes
3 answers
2
go_editor asked Sep 28, 2014
9,909 views
The number of distinct positive integral factors of $2014$ is _____________
34 votes
34 votes
3 answers
4
go_editor asked Sep 28, 2014
11,244 views
Which of the following socket API functions converts an unconnected active TCP socket into a passive socket?$\textsf{connect}$$\textsf{bind}$$\textsf{listen}$$\textsf{acc...