Minimum number of queues to implement stack is ___________
Question
Minimum number of queues to implement stack is ___________
Solution
The minimum number of queues needed to implement a stack is two.
Here is the step by step process:
-
Push operation:
- Enqueue in the first queue.
-
Pop operation:
- While the size of the first queue is more than 1, pipe dequeued items from the first queue into the second queue.
- Dequeue the last item of the first queue, this is the popped element.
- Swap the names of the first queue and the second queue.
In this way, by using two queues, we can implement a stack.
Similar Questions
To implement a stack using queue(with only enqueue and dequeue operations), how many queues will you need?Options2134
How many stacks are needed to implement a queue? Consider the situation where no other data structure like arrays, linked list is available to you.ans.
Implement Stack using Queues
to implement a stack using queue how many queues will you need a)2b(3c)1d)4
One difference between a queue and a stack
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.