Quote Originally Posted by Salem View Post
A stack is a LIFO (Last In, First Out) data structure.
That is, you 'push' and 'pop' from just one end.

A queue on the other hand is a FIFO (First In, First Out) data structure.
You 'push' on one end, and 'pop' off the other.

So every time you pop from your synthesised queue, you need some way to get at the first thing you pushed.
I guess the question is, do you prefer to do all this stack reversal when you push or when you pop?
Actually the second_stack is used here as the reversed form of the stack.
To my understanding, as long as the stack has a peek - it is placed at the top of the second stack.
So, when we would like to pop an element from the queue, we approach to the second stack - popping its "upper" element up.
When we would like to push an element to the queue - we just enqueue it to the first stack - its turn will be soon as the second stack happens to be empty again.