• 0

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 :