You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
912 B

4 years ago
  1. function Queue () {
  2. this.head = new Item('head', null)
  3. }
  4. module.exports = Queue
  5. Queue.prototype.append = function append (kind, value) {
  6. var item = new Item(kind, value)
  7. this.head.prepend(item)
  8. return item
  9. }
  10. Queue.prototype.isEmpty = function isEmpty () {
  11. return this.head.prev === this.head
  12. }
  13. Queue.prototype.first = function first () {
  14. return this.head.next
  15. }
  16. function Item (kind, value) {
  17. this.prev = this
  18. this.next = this
  19. this.kind = kind
  20. this.value = value
  21. }
  22. Item.prototype.prepend = function prepend (other) {
  23. other.prev = this.prev
  24. other.next = this
  25. other.prev.next = other
  26. other.next.prev = other
  27. }
  28. Item.prototype.dequeue = function dequeue () {
  29. var prev = this.prev
  30. var next = this.next
  31. prev.next = next
  32. next.prev = prev
  33. this.prev = this
  34. this.next = this
  35. return this.value
  36. }
  37. Item.prototype.isEmpty = function isEmpty () {
  38. return this.prev === this
  39. }