-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
73 lines (61 loc) · 1.66 KB
/
queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//namespace
this.particlejs = this.particlejs || {};
//class
(function() {
"use strict";
//constructor
const Queue = function() {
this.head = null;
this.previousNode = null;
this.currentNode = null;
this.length = 0;
};
const proto = Queue.prototype;
proto.begin = function() {
if (this.head) {
this.previousNode = null;
this.currentNode = this.head;
return this.head.value;
}
return null;
};
proto.next = function() {
if (this.currentNode && this.currentNode.next) {
this.previousNode = this.currentNode;
this.currentNode = this.currentNode.next;
return this.currentNode.value;
}
return null;
};
proto.end = function() {
if (this.begin() !== null) {
while (this.next()) {}
return this.currentNode.value;
}
return null;
};
proto.enqueue = function(obj) {
const node = {
value: obj,
next: this.head,
previous: null
};
this.head = node;
++this.length;
};
proto.dequeue = function() {
if (this.end() !== null) {
const node = this.currentNode;
if (this.previousNode) {
this.previousNode.next = null;
}
--this.length;
if (this.length === 0) {
this.head = this.currentNode = this.previousNode = null;
}
return node.value;
}
return null;
};
particlejs.Queue = Queue;
})();