def hot_potato(name_list, times): name_queue = MyQueue() for name in name_list: name_queue.enqueue(name) while name_queue.size() > 1: for i in range(times): name_queue.enqueue(name_queue.dequeue()) name_queue.dequeue() return name_queue.dequeue()
def bfs(start): # start.distance = 0 # start.previous = None vertex_queue = MyQueue() vertex_queue.enqueue(start) while vertex_queue.size() > 0: current = vertex_queue.dequeue() for nbr in current.connections: if nbr.state == 'unprocessed': nbr.state = 'processing' nbr.distance = current.distance + 1 nbr.previous = current vertex_queue.enqueue(nbr) current.state = 'processed'
from my_queue import MyQueue k1 = MyQueue(4) k1.add(3) k1.add(3123) k1.add(3) k1.add(123) k1.add(4) k1.add(0) k1.add(2) k1.add(1) print(k1.size()) k1.remove() k1.remove() k1.remove() k1.remove() k1.remove() k1.remove()