用于创建LIFO队列的Python程序

时间:2018-05-04 06:44:38

标签: python algorithm fifo

这是我想出的答案,但我在网上找到了这个答案。

     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/

1 个答案:

答案 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.