The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+28 votes
4.5k 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.
asked in DS by Veteran (106k points)
edited by | 4.5k views
+1

6 Answers

+37 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$)

answered by Veteran (363k 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!!!
+5
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??
+17 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.
answered by Boss (43k 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
2nd image Caption
+13 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

answered by Loyal (7.5k points)
0
Good Explanation
+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====>>

STACK
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.2k 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 (8.6k points)
0
Please someone explain diagrammatically why option B is wrong
–3 votes
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??

Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,599 questions
48,601 answers
155,672 comments
63,739 users