top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How can I implement Stack using Queue?

+3 votes
596 views
How can I implement Stack using Queue?
posted Apr 23, 2014 by Atul Mishra

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

If I got your query correctly.
Please do the insertion and deletion operations from the rear end of the queue then it behaves as a stack.

answer Apr 23, 2014 by Vimal Kumar Mishra
0 votes

You will require two queue if you want production grade solution, and can e achieve in two ways:

Method 1:
push operation of the Stack: enqueue in queue1

pop operation of the Stack: while size of queue1 is bigger than 1, pipe dequeued items from queue1 into queue2; dequeue and return the last item of queue1, then switch the names of queue1 and queue2

Method 2:
push operation of the Stack: enqueue in queue2; enqueue all items of queue1 in queue2, then switch the names of queue1 and queue2

pop operation of the Stack: deqeue from queue1

answer Apr 23, 2014 by Salil Agrawal
...