queue.js 486 B

123456789101112131415161718192021222324
  1. var Queue = function () {
  2. this.head = null;
  3. this.tail = null;
  4. };
  5. Queue.prototype = {
  6. add: function (item) {
  7. var entry = { item: item, next: null };
  8. var tail = this.tail;
  9. if (tail) tail.next = entry;
  10. else this.head = entry;
  11. this.tail = entry;
  12. },
  13. get: function () {
  14. var entry = this.head;
  15. if (entry) {
  16. var next = this.head = entry.next;
  17. if (next === null) this.tail = null;
  18. return entry.item;
  19. }
  20. }
  21. };
  22. module.exports = Queue;