Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. var head:Int = 0
  2. var num:Int = 0
  3. var max:Int = 0
  4. var data:[Any]!
  5. var debug = false
  6.  
  7. init(_ max:Int) {
  8. self.max = max
  9. self.data = Array<Any>(repeating: -1, count: max)
  10. }
  11.  
  12. @discardableResult
  13. func enqueue(_ obj:Any) -> Bool { // キューが一杯だったらfalseを返す
  14. if num < max {
  15. self.data[(head + num) % max] = obj
  16. num = num + 1
  17. return true
  18. }
  19. return false
  20. }
  21.  
  22. @discardableResult
  23. func dequeue() -> Any? {
  24. if num > 0 {
  25. let obj = data[head]
  26. data[head] = -1
  27. num = num - 1
  28. head = (head + 1) % max;
  29. return obj
  30. }
  31. return nil
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement