FIFO:先进先出
# 采用list来容纳queue的数据项
class queue:
'定义queue类'
def __init__(self):
self.items = []
def isempty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
# 应用:热土豆,穿过num个人,队首的人出列
def hotpotato(num, names):
q = queue() # 实例queue类
for i in names:
q.enqueue(i)
while q.size() >= 1:
for j in range(num):
q.enqueue(q.dequeue())
j += 1
q.dequeue()
return q.isempty()
print(hotpotato(7, ["lisa", 'rita', 'linlin', 'daming', 'amy','dfs']))