Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: - void push(int x) Pushes element x to the top of the stack. - int pop() Removes the element on the top of the stack and returns it. - int top() Returns the element on the top of the stack. - boolean empty() Returns true if the stack is empty, false otherwise. Notes: You must use only standard operations of a queue. Depending on your language, the queue may not be supported natively.
Example 1
Input: operations = ["MyStack","push","push","top","pop","empty"], arguments = [[],[1],[2],[],[],[]]
Output: [null,null,null,2,2,false]
Explanation: 1 and 2 are pushed. top() and pop() both return 2 because a stack serves the most recent element first. 1 is still inside, so empty() is false.
Example 2
Input: operations = ["MyStack","empty"], arguments = [[],[]]
Output: [null,true]
Explanation: Nothing has been pushed, so the stack is empty.
Example 3
Input: operations = ["MyStack","push","pop","empty"], arguments = [[],[5],[],[]]
Output: [null,null,5,true]
Explanation: 5 is pushed and popped, leaving the stack empty.
A queue is FIFO, but a stack needs LIFO. After enqueueing a new element, rotate all the older elements behind it so the newest sits at the front — then front-of-queue behaves like top-of-stack.
Run checks all cases above. Submit evaluates all test cases.