fork(1) download
  1. dataQ = {}
  2. dataQ.first = 0
  3. dataQ.last = -1
  4. dataQ.data = {}
  5.  
  6. function insert(q, val)
  7. q.last = q.last + 1
  8. q.data[q.last] = val
  9. end
  10.  
  11. function remove(q)
  12. if (q.first > q.last) then
  13. rval = -1
  14. else
  15. print("remove: q.data[q.first]= ", q.data[q.first], " q.first= ", q.first)
  16. local rval = q.data[q.first]
  17. print("remove: rval= ", rval)
  18. q.data[q.first] = nil -- to allow garbage collection
  19. q.first = q.first + 1
  20. print("remove: q.first= ", q.first)
  21. end
  22. return rval
  23. end
  24.  
  25. insert(dataQ,"a")
  26. insert(dataQ,"b")
  27. insert(dataQ,"c")
  28. for i,v in ipairs(dataQ.data) do
  29. print(i, v)
  30. end
  31. repeat
  32. local x = remove(dataQ)
  33. print("list item= ", x)
  34. until (dataQ.first > dataQ.last)
  35.  
  36. -- your code goes here
Success #stdin #stdout 0s 2788KB
stdin
Standard input is empty
stdout
1	b
2	c
remove: q.data[q.first]= 	a	  q.first= 	0
remove: rval= 	a
remove: q.first= 	1
list item= 	nil
remove: q.data[q.first]= 	b	  q.first= 	1
remove: rval= 	b
remove: q.first= 	2
list item= 	nil
remove: q.data[q.first]= 	c	  q.first= 	2
remove: rval= 	c
remove: q.first= 	3
list item= 	nil