-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathqueue.js
40 lines (35 loc) · 785 Bytes
/
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
var Node = function(value) {
this.prevNode = null;
this.value = value;
}
var Queue = function() {
this.first = null;
this.last = null;
this.length = 0;
}
Queue.prototype.push = function(value) {
var node = new Node(value);
if(this.length !== 0) {
this.last.prevNode = node;
this.last = node;
} else {
this.last = node;
this.first = node;
}
this.length++;
}
Queue.prototype.pop = function() {
if(this.length === 0)
return null;
else if(this.length === 1) {
var node = this.first;
this.first = null;
this.last = null;
this.length--;
return node.value;
}
var node = this.first;
this.first = this.first.prevNode;
this.length--;
return node.value;
}