队列的应用:热土豆
作者:互联网
队列的应用:热土豆
1.内容
2.练习
"""
Queue() 创建一个空队列
enqueue(item) 插入队尾
dequeue() 返回队首元素
isEmpty() 返回是否为空
size() 返回队列数据项个数
"""
class Queue:
def __init__(self):
self.items = []
def enqueue(self, item):
self.items.insert(0, item)
def dequeue(self):
return self.items.pop()
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
if __name__ == '__main__':
q = Queue()
print(q.isEmpty())
q.enqueue(4)
q.enqueue('dog')
q.enqueue(True)
print(q.size())
print(q.isEmpty())
q.enqueue(8.4)
print(q.dequeue())
print(q.dequeue())
print(q.size())
from pythonds.basic.queue import Queue
def hotPotato(name_list, num):
queue = Queue()
for name in name_list:
queue.enqueue(name)
while queue.size() > 1:
for i in range(num):
queue.enqueue(queue.dequeue())
queue.dequeue()
return queue.dequeue()
if __name__ == '__main__':
name_list = ['旋涡鸣人', '宇智波佐助', '小樱', '小李', '旗木卡卡西', '大蛇丸', '纲手', '自来也', '宇智波鼬', '兜']
print(hotPotato(name_list, 3))
print(hotPotato(name_list, 5))
print(hotPotato(name_list, 7))
旗木卡卡西
小樱
旋涡鸣人
标签:__,name,队列,self,queue,enqueue,应用,print,土豆 来源: https://blog.csdn.net/weixin_45462577/article/details/115715105