source: trip-planner-front/node_modules/handle-thing/lib/queue.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 912 bytes
Line 
1function Queue () {
2 this.head = new Item('head', null)
3}
4module.exports = Queue
5
6Queue.prototype.append = function append (kind, value) {
7 var item = new Item(kind, value)
8 this.head.prepend(item)
9 return item
10}
11
12Queue.prototype.isEmpty = function isEmpty () {
13 return this.head.prev === this.head
14}
15
16Queue.prototype.first = function first () {
17 return this.head.next
18}
19
20function Item (kind, value) {
21 this.prev = this
22 this.next = this
23 this.kind = kind
24 this.value = value
25}
26
27Item.prototype.prepend = function prepend (other) {
28 other.prev = this.prev
29 other.next = this
30 other.prev.next = other
31 other.next.prev = other
32}
33
34Item.prototype.dequeue = function dequeue () {
35 var prev = this.prev
36 var next = this.next
37
38 prev.next = next
39 next.prev = prev
40 this.prev = this
41 this.next = this
42
43 return this.value
44}
45
46Item.prototype.isEmpty = function isEmpty () {
47 return this.prev === this
48}
Note: See TracBrowser for help on using the repository browser.