queue.js 465 B

1234567891011121314151617181920212223
  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. if (this.head) this.tail.next = entry;
  9. else this.head = entry;
  10. this.tail = entry;
  11. },
  12. get: function () {
  13. var entry = this.head;
  14. if (entry) {
  15. this.head = entry.next;
  16. if (this.tail === entry) this.tail = null;
  17. return entry.item;
  18. }
  19. }
  20. };
  21. module.exports = Queue;