1 | function Queue () {
|
2 | this.head = new Item('head', null)
|
3 | }
|
4 | module.exports = Queue
|
5 |
|
6 | Queue.prototype.append = function append (kind, value) {
|
7 | var item = new Item(kind, value)
|
8 | this.head.prepend(item)
|
9 | return item
|
10 | }
|
11 |
|
12 | Queue.prototype.isEmpty = function isEmpty () {
|
13 | return this.head.prev === this.head
|
14 | }
|
15 |
|
16 | Queue.prototype.first = function first () {
|
17 | return this.head.next
|
18 | }
|
19 |
|
20 | function Item (kind, value) {
|
21 | this.prev = this
|
22 | this.next = this
|
23 | this.kind = kind
|
24 | this.value = value
|
25 | }
|
26 |
|
27 | Item.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 |
|
34 | Item.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 |
|
46 | Item.prototype.isEmpty = function isEmpty () {
|
47 | return this.prev === this
|
48 | }
|