6,017 views
6 votes
6 votes
A queue is implemented using 2 stacks. Minimum no. of stack operations (PUSH and POP) required for the sequence of 3 insertions and 2 delete operations in the queue is -------  .

2 Answers

8 votes
8 votes

Suppose 3 insertions are A,B,C Queue is FIFO ordered data structure but according to (i) it is not FIFO its LIFO

SO we popped out all elements from 1 stack to another as (ii) now if we pop elements it will follow FIFO order 

So 

Push  Pop 
 (in Stack 1) 3 (A,B,C)  3(C,B,A)
( in Stack 2) 3 (C,B,A)  2 (as mentioned) A,B
Total 6  5
Push + pop = 11

Reference :-https://www.youtube.com/watch?v=x0_9uVLQ7SI

0 votes
0 votes
First push 3 elements in stack 1- 3 Push

Then pop and push them in 2nd stack- 3 Pop+3Push

Then pop 2 elements- 2 Pop

Total=11

Related questions

2 votes
2 votes
2 answers
2
7 votes
7 votes
1 answer
3
junaid ahmad asked Oct 31, 2017
3,969 views
Q.What will be time complexity of enqueue and dequeue operation when a queue is implemented using two stacks.