queue.lua 834 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. local Queue = function()
  2. local queue = {};
  3. local tail = 0;
  4. local head = 0;
  5. local public = {};
  6. public.push = function(obj)
  7. queue[head] = obj;
  8. head = head + 1;
  9. return;
  10. end
  11. public.pop = function()
  12. if tail < head
  13. then
  14. local obj = queue[tail];
  15. queue[tail] = nil;
  16. tail = tail + 1;
  17. return obj;
  18. else
  19. return nil;
  20. end
  21. end
  22. public.size = function()
  23. return head - tail;
  24. end
  25. public.getHead = function()
  26. return head;
  27. end
  28. public.getTail = function()
  29. return tail;
  30. end
  31. public.reset = function()
  32. queue = {};
  33. head = 0;
  34. tail = 0;
  35. end
  36. return public;
  37. end
  38. return Queue;