5.3k views

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

1. A queue cannot be implemented using this stack.
2. A queue can be implemented where $ENQUEUE$ takes a single instruction and $DEQUEUE$ takes a sequence of two instructions.
3. A queue can be implemented where $ENQUEUE$ takes a sequence of three instructions and $DEQUEUE$ takes a single instruction.
4. A queue can be implemented where both $ENQUEUE$ and $DEQUEUE$ take a single instruction each.
edited | 5.3k views
+1

(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$)

answered by Veteran (385k points)
edited by
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
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,
0
or anybody else here plzz explain it
0
@Aman, Neha clear now? The  sentence was not complete earlier..
0
@Arjun sir..yes sir
0
yes..
0
ohh .. cleared.. thnks sir
0
@arjun sir

tell what should b do in exam when such type of question will come ?
0
Sir, it can be either way as well. 1 instruction for enqueue, three instructions for dequeue.
0
can anyone explain it nicely with an examle..m unable to understand...what is happening here??
0
For the option B... How it is taking 3 instructions..
Step1:reverse
Step2:pop
Then for next pop
We can simply perform pop operation.. We are we again reversing ??
0
Similarly can we say that Deque can be done in 3 instruction while enque in one instruction.
ENQUEUE -> REVERSE the stack, PUSH the element and then again REVERSE the stack.

For DEQUEUE we simply POP the element.

So answer is C.

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.
answered by Boss (43.5k points)
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.
+1
can anyone explain it diagrammatically???
0
Caption

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

answered by Loyal (7.8k points)
0
Good Explanation
0

https://gateoverflow.in/684/gate2000-13...in this question said ENQUEUE take single operation..?

0
To DEQUEUE an item , can we first Reverse the stack and then POP it takes 2 operations. is it correct?

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

answered by Boss (16.4k points)
edited by
0
@sourav. So the steps for enqueue are reverse the stack, pop then reverse again, as you say.

But in Arjun sir's answer, we have reverse,push(and not pop) then reverse.

Could you please clarify the ambiguity.
+2
its reverse push reverse, take small example for this. if you pop then you have to do two push , since one for you pop and one for new element
+1 vote
To DEQUEUE an item, simply POP. To ENQUEUE an item, we can do following 3 operations 1) REVERSE 2) PUSH 3) REVERSE
answered by Loyal (9.3k points)
0
Please someone explain diagrammatically why option B is wrong

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)

answered by Junior (593 points)
D....
answered by (137 points)
0
how?
0

@Arjun sir

If the REVERSE instruction is given, so we can implement Queue using only one stack??

1
2