This solution adopting only one queue to store data and caching the top element to make method top time complexity constant.

Time complexity for each method.

push O(1)

pop O(n)

top O(1)

empty O(1)

```
private Queue<Integer> queue = new ArrayDeque<>();
private Integer topElement = null;
// Push element x onto stack.
public void push(int x) {
topElement = x;
queue.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
Integer element = null;
for (int i = 0, size = queue.size() - 1; i < size; i++) {
element = queue.poll();
queue.offer(element);
}
topElement = element;
queue.poll();
}
// Get the top element.
public int top() {
return topElement;
}
// Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
```