Similar --> https://gateoverflow.in/684/gate2000-13

The Gateway to Computer Science Excellence

+32 votes

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*)?

- A queue cannot be implemented using this stack.
- A queue can be implemented where $ENQUEUE$ takes a single instruction and $DEQUEUE$ takes a sequence of two instructions.
- A queue can be implemented where $ENQUEUE$ takes a sequence of three instructions and $DEQUEUE$ takes a single instruction.
- A queue can be implemented where both $ENQUEUE$ and $DEQUEUE$ take a single instruction each.

+53 votes

Best answer

(**C**) is the answer. While $ENQUEUE$ we $REVERSE$ the stack, $PUSH$ the element and then again $REVERSE$ the stack. For $DEQUE$ we simply $POP$ the element.

(Option (B) can be used to get the first element from the stack by doing a $POP$ after $REVERSE$ for $DEQUEUE$ and $PUSH$ for $ENQUEUE$. But we have to restore the stack using $REVERSE$ (otherwise next $POP$ won't work) which means $DEQUEUE$ actually needs $3$ instructions and not $2$)

0

sir,if we would dequeue,we must dequeue from last not first...but as it is stack how dequeue takes only one operation...i mean to say if it does pop it would from first as in case of stack FIFO but queue follows LIFO,!!!!please help me out arjun sir!!!

+8

In C option while adding element we put it at the bottom of the stack, so the top of the stack is actually the first inserted element. So, a simple POP is doing a FIFO here.

Queue - FIFO

Stack - FILO or LIFO

Queue - FIFO

Stack - FILO or LIFO

0

Sir for B u said "next pop wont work" . Shouldn't it be "next push wont work" ? As we reverse the stack we can get items in the order they were inserted but once the stack has been reversed , We cannot the maintain the same order for enque as for queue. Please correct if i am wrong.

0

@Arjun sir..plzz elaborate this solution that what happend when reverese of stack n the push n then again reverse..its not clear to me,

+35 votes

suppose queue is:

1 |
2 |
3 |
4 |
5 |

stack representation will be:

5 |

4 |

3 |

2 |

1 |

Reverse the stack

STACK |

1 |

2 |

3 |

4 |

5 |

Now, To DEQUEUE an item, simply POP. For eg. If we want to delete 1 from queue then we simply pop the top element of stack, which is 1.

To ENQUEUE an item, we can do following 3 operations 1) REVERSE 2) PUSH 3) REVERSE

For eg. If we want to add 6 in queue then first we reverse the stack

5 |

4 |

3 |

2 |

1 |

Push 6

6 |

5 |

4 |

3 |

2 |

1 |

And then again reverse the stack

1 |

2 |

3 |

4 |

5 |

6 |

hence ans : C

+25 votes

ENQUEUE -> REVERSE the stack, PUSH the element and then again REVERSE the stack.

For DEQUEUE we simply POP the element.

So answer is C.

Additional Information->

We can also implement queue can be implemented where DEQUEUE takes a sequence of three instructions and ENQUEUE takes a single instruction.

While Doing ENQUEUE we just PUSH.while doing DEQUEUE we first REVERSE, Then POP, Then again REVERSE.

For DEQUEUE we simply POP the element.

So answer is C.

Additional Information->

We can also implement queue can be implemented where DEQUEUE takes a sequence of three instructions and ENQUEUE takes a single instruction.

While Doing ENQUEUE we just PUSH.while doing DEQUEUE we first REVERSE, Then POP, Then again REVERSE.

0

so whats the answer?? c or D i think one of the function will take 3 operation and other one will take one operation.

+2

ANswer is C. I gave other implementation just for more info. See that I've written in my answer as ANSWER is C.

0

I guess, the part additional information is what comes to mind first....I dont understand why Enqueue would take 3 instruction at all. If Queue has 1,2,3,4,5 elements....then stack will contain elements also in the order $,1,2,3,4,5 <- top of stack

Hence Enqueue would take one 1 PUSH operation

Please someone clarify

Hence Enqueue would take one 1 PUSH operation

Please someone clarify

+5 votes

Consider a** Stack** element {1,2,3,4,5} given by below==>>

5 |

4 |

3 |

2 |

1 |

To Implement **Queue** using **Stack** we have three operation given as ===>

** Operation 1.Reversing it we get=**===>>

1 |

2 |

3 |

4 |

5 |

*Operation 2 Followed by Poping===>>*

{1,2,3,4,5}

*Operation 3 Followed by Enqueing===>>>*

1 | 2 | 3 | 4 | 5 |

From the Above Diagram we can conclude that C) is true

+1 vote

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

0 votes

**ENQUEUE** using simple PUSH operation and **DEQUEUE**(REV,POP,REV)

0 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.

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.

–4 votes

52,215 questions

59,981 answers

201,180 comments

94,641 users