## 题目描述：

Implement the following operations of a stack using queues.

push(x) -- Push element x onto stack.
pop() -- Removes the element on top of the stack.
top() -- Get the top element.
empty() -- Return whether the stack is empty.

Notes:
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue -- which means only push to back, pop from front, size, and is empty operations are valid.

## 题目大意：

push(x) -- 将元素x压入栈.
pop() -- 移除栈顶元素.
top() -- 获得栈顶元素.
empty() -- 返回栈是否为空.

## 解题思路：

push(x) -- 使用queue的push to back操作.
pop() -- 将queue中除队尾外的所有元素pop from front然后push to back，最后执行一次pop from front
top() -- 将queue中所有元素pop from front然后push to back，使用辅助变量top记录每次弹出的元素，返回top
empty() -- 使用queue的is empty操作.

## Python代码：

class Stack:
# initialize your data structure here.
def __init__(self):
self.queue = []

# @param x, an integer
# @return nothing
def push(self, x):
self.queue.append(x)

# @return nothing
def pop(self):
for x in range(len(self.queue) - 1):
self.queue.append(self.queue.pop(0))
self.queue.pop(0)

# @return an integer
def top(self):
top = None
for x in range(len(self.queue)):
top = self.queue.pop(0)
self.queue.append(top)

# @return an boolean
def empty(self):
return self.queue == []

Pingbacks已关闭。