Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Reddit
Contact us
Hide Buttons

Queue using two stacks javascript

Problem :

Implement queue’s enqueue and dequeue operations using two stacks.

Logic:

  • The queue is first-in-first-out and a stack is last in-first-out phenomena. Hence, the main difference between these two data structures is that pop() in reverse order.
  • Use one stack to store all the pushed elements and use the second stack to store elements in reverse the order. (by popping s1 and pushing
    the elements on to s2).
  • We will push elements to s1 and pop from s2.

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *