1,202 views
1 1 vote

I have a doubt regarding " implementing queue using 2 stacks "
  For an EQueue operation there is a push operation in one of the stack  that is OK
but when we are doing first DQueue operation why we need to pop each element from from one stack and push all into other stack rather than pop elements from one stack until it has one element and after that pop that element from the same stack .

1 Answer

1 1 vote
Because if you won't push them onto 2nd stack you will lose elements after first one.  So when you perform dequeue next time from where will second element come so that we can dequeue it
Position:
Show:

Related questions

7 7 votes
3 3 answers
319
319 views
GO Classes asked Jul 8
319 views
A queue is implemented using two stacks $\text{S1}$ and $\text{S2}$.Use the implementation where $\texttt{dequeue()}$ is $\text{O(1)}$ by keeping the front of the queue a...
6 6 votes
1 answers 1 answer
5.6k
5.6k views
junaid ahmad asked Oct 31, 2017
5,604 views
Q.What will be time complexity of enqueue and dequeue operation when a queue is implemented using two stacks.
1 1 vote
2 2 answers
3.0k
3.0k views
Tushar Shinde asked Dec 7, 2015
3,007 views
Which of the following statement is/are correct?1. If we use only Front pointer in queue, then insertion takes O(n) time while deletion in O(1) time.2. If we use only Rea...
3 3 votes
1 1 answer
2.4k
2.4k views
Parshu gate asked Nov 13, 2017
2,445 views
PLEASE PROVIDE SOURCE FOR THIS CONCEPTSuppose that queue operations are implemented using stack operation. enqueue(x) and Dequeue( ) are queue operations whereas Pop( ) a...