Maintenance Mode

Have Patience Working on it

Implement a Queue Using two stacks

 Implement a Queue using two stacks




Stack helps us to reverse the elements. So, if we use two stacks to reverse the same elements, we end up to the original order.

Let we are using two stacks S1 and S2 to implement of a queue.

1. When we need to add elements enQueue (push) it to stack S1.
2. When we need to remove elements deQueue from S2, two cases arrises:-
    (a) If the stack S2 is empty then we will pop elements from stack S1 and add         to stack S2.
    (b) If the stack S2 is not empty we simply pop from stack S2.


 

Implement a Queue Using two stacks Implement a Queue Using two stacks Reviewed by Leaf Code on September 10, 2020 Rating: 5

No comments:

SET ADT Using Students courses example

Powered by Blogger.