Friday, November 24, 2023

What is the minimum number of stacks needed to implement a queue?


Answer: Two

A queue can be implemented using two stacks.

A queue follows the order of FIFO (First in First out). To obtain this order using Stack, which follows FILO (First in Last out) we need two stacks.

Let M and N are two stacks

Enqueue: Push new element onto M.

Dequeue: If N is empty, fill it by popping each element from M and push it onto N.

Now pop and return the top element from N. This gives the FIFO order

0 comments:

Post a Comment

Data Structures with C++



NET/SET/CS PG



Operating Systems



Computer Networks



JAVA



Design and Analysis of Algorithms



Programming in C++

Top