GATE2014-2-41

11.7k 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.
in DS
edited
2

We can implement the queue operations in either of these two ways:-

1. Reverse, push, reverse; and pop.
2. Reverse, pop, reverse; and push.

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

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!!!
10
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..
1
@Arjun sir..yes sir
1
yes..
1
ohh .. cleared.. thnks sir
1
@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 ??
1
Similarly can we say that Deque can be done in 3 instruction while enque in one instruction.
0

Arjun Ideal answer for this question should be 2 operations for Enqueue (Reverse+Push) and 2 operations for Dequeue(Reverse+Pop). It will work for all the cases

0

@shetu_raj It will not work ...consider

Enqueue 1      stack      1

Enqueue 2      stack      2  1

Enqueue 3       stack     3 1  2   ( reverse  then push)

Dequeue       gives you 2.  (reverse+pop)

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

1
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?
0
Thanks for the explanation.It really helps me to clear my concept.
0
GOOD EXP
0
Thanks...
0
Nice Explanation.
0
These type of questions needs diagrammatically explanation, very nicely done. @rishu_darkshadow
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.
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
0