5,581 views
6 6 votes
Q.What will be time complexity of enqueue and dequeue operation when a queue is implemented using two stacks.

1 Answer

Best answer
7 7 votes

The time complexity of enqueue and dequeue operation when using 2 stacks

If here enqueue using 2 stacks, then dequeue will use 1 stack is enough

Say, for enqueue we use reverse, push , reverse operations

  • So, in reversing all element will push in another stack take O(n) time
  • New element will be push ,take O(1) time
  • Then push back to the 1st stack will take O(n) time

Now in case of dequeue

  • just do pop the element from the front of stack(It will just take O(1) time)

_____________________________________________________________________

We can also do in reverse way, that take enqueue operation in O(1) time and dequeue O(n) time

• edited by
Position:
Show:

Related questions

7 7 votes
3 3 answers
317
317 views
GO Classes asked Jul 8
317 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...
3 3 votes
1 1 answer
2.4k
2.4k views
Parshu gate asked Nov 13, 2017
2,441 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...
2 2 votes
1 1 answer
1.9k
1.9k views
Parshu gate asked Nov 11, 2017
1,851 views
Suppose Stack operations are implemented using Queue Operation. Push(x) and Pop() are stack operations whereas enqueue(x) and dequeue are Queue operations.Consider the co...
6 6 votes
2 2 answers
6.7k
6.7k views
SSrawat asked Oct 9, 2017
6,712 views
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 ...