fork download
  1. # Queue.py
  2. # Mechanical MOOC MIT OCW 6.189 Exercise 3.6
  3. # List implementation of a Queue
  4. # Glenn A. Richard
  5. # August 2, 2013
  6. class Queue(object):
  7. def __init__(self):
  8. # Initialize the Queue to empty
  9. self.items = []
  10. def insert(self, item):
  11. # Append the item to the back of the Queue
  12. self.items.append(item)
  13. def remove(self):
  14. if len(self.items) == 0:
  15. # The Queue had no items in it
  16. return "The Queue is empty"
  17. else:
  18. # The Queue was not empty; remove and return the item at the front
  19. return self.items.pop(0)
  20.  
  21. queue = Queue()
  22. queue.insert(5)
  23. queue.insert(6)
  24. print queue.remove()
  25. # 5
  26. queue.insert(7)
  27. print queue.remove()
  28. # 6
  29. print queue.remove()
  30. # 7
  31. print queue.remove()
  32. # The queue is empty
  33.  
Success #stdin #stdout 0.01s 7728KB
stdin
Standard input is empty
stdout
5
6
7
The Queue is empty