155. Implement Stack using Queue
Implement a Last-In-First-Out (LIFO) stack using a single queue. The implemented stack should support the following operations: push, pop, top, and isEmpty.
Implement the QueueStack class:
void push(int x): Pushes element x onto the stack.
int pop(): Removes and returns the top element of the stack.
int top(): Returns the top element of the stack without removing it.
boolean isEmpty(): Returns true if the stack is empty, false otherwise.
Example 1:
Input:
["QueueStack", "push", "push", "pop", "top", "isEmpty"]
[[], [4], [8], [], [], []]
Output: [null, null, null, 8, 4, false]
Explanation:
QueueStack stack = new QueueStack();
stack.push(4);
stack.push(8);
stack.pop(); // returns 8
stack.top(); // returns 4
stack.isEmpty(); // returns false
Example 2:
Input:
["QueueStack", "isEmpty"]
[[]]
Output:[null, true]
Explanation:
QueueStack stack = new QueueStack();
stack.isEmpty(); // returns true
Now Your Turn!
Pick the correct output for the given inputInput:
["QueueStack", "push", "pop", "isEmpty"]
[[], [6], [], []]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= numbers of calls made <= 100
- 1 <= x <= 100