johnmahugu

python - Queues (FIFO) and stacks (LIFO)

Jun 14th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.73 KB | None | 0 0
  1. """
  2. Queues (FIFO) and stacks (LIFO)
  3.  
  4. Python makes using queues and stacks a piece of cake (Did I already say "piece of cake" ?).
  5. No use creating a specific class: simply use list objects.
  6. """
  7. For a stack (LIFO), stack with append() and destack with pop():
  8. >>> a = [5,8,9]
  9. >>> a.append(11)
  10. >>> a
  11. [5, 8, 9, 11]
  12. >>> a.pop()
  13. 11
  14. >>> a.pop()
  15. 9
  16. >>> a
  17. [5, 8]
  18. >>>
  19.  
  20.  
  21. For a queue (FIFO), enqueue with append() and dequeue with pop(0):
  22. >>> a = [5,8,9]
  23. >>> a.append(11)
  24. >>> a
  25. [5, 8, 9, 11]
  26. >>> a.pop(0)
  27. 5
  28. >>> a.pop(0)
  29. 8
  30. >>> a
  31. [9, 11]
  32.  
  33. """
  34. As lists can contain any type of object, you an create queues and stacks of any type of objects !
  35.  
  36. (Note that there is also a Queue module, but it is mainly usefull with threads.)
  37. """
Advertisement
Add Comment
Please, Sign In to add comment