474 views
2 votes
2 votes
A stack is implemented with two queues then what are the minimum enqueue and dequeue operations needed to perform for pop operation where n is the number of elements already in the first queue.?

1 Answer

2 votes
2 votes

Assuming queue1 is used for performing push operation in O(1) time, we'll require n dequeue and (n - 1) enqueue operations to pop an element. It can be done as :

1) dequeue from queue 1

2) if(queue 1 !empty) enqueue in queue 2

 else return that element ( our pop operation )

For different implementations of push and pop operation refer this.

Related questions

0 votes
0 votes
1 answer
1
Abhrajyoti00 asked Oct 29, 2022
718 views
Can there be “Stack Overflow” in Linked list Implementation of stack? If Yes, how?