Takefumi Yamamura's blog

This blog is for my memorandum about programming and English.

Happy Coding

This blog is for my memorandum

Queue via Stacks


Implement a MyQueue class which implements a queue using two stacks

how to solve

I implement two functions pop and push. The first one is getting and removing the oldest element in MyQueue. The second one is adding the element into MyQueue

Firstly, I implement push function in the same way as a stack. Secondly, I implement pop function by using second list. I can use the second stack to reverse the order of the elements in first stack. I push the elements of stack into the second stack
until the stack become to be an empty and then I can get the reverse order stack. Next, I pop the second stack and I add the elements of the remainder of the second stack into the first stack. I can get the stack which of the oldest element is deleted.