这是我想出的答案,但我在网上找到了这个答案。
import queue
q = queue.LifoQueue()
for x in range(4):
q.put(str(x))
while not q.empty():
print(q.get(), end=" ")
print()
这是Java版本,我想要python版本: https://leetcode.com/problems/implement-queue-using-stacks/solution/
答案 0 :(得分:1)
在FIFO结构中,您可以使用收集库中的deque类使用先进先出逻辑来执行此操作
l2=[3,5,7,8]
l2.append(35)
l2.popleft() # The first incoming data comes out with. So the data on the left.