In [1]:
"""
Implementation of queue in Python (with List)
Source:
http://interactivepython.org/runestone/static/pythonds/BasicDS/ImplementingaQueueinPython.html
"""
class Queue:
def __init__(self):
"""initialize queue with empty list
"""
self.items = []
def isEmpty(self):
"""check if it is empty
"""
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)
def hotPotato(namelist, num):
simqueue = Queue()
for name in namelist:
simqueue.enqueue(name)
while simqueue.size() > 1:
for i in range(num):
simqueue.enqueue(simqueue.dequeue())
simqueue.dequeue()
return simqueue.dequeue()
if __name__ == "__main__":
q = Queue()
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 "hot potato"
print hotPotato(["Bill", "David", "Susan", "Jane", "Kent", "Brad"], 7)
In [ ]: