-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.js
35 lines (30 loc) · 853 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
// This Stack is written using the pseudoclassical pattern
// Creates the queue
var Queue = function() {
this.storage = {};
this.count = 0;
this.lowestCount = 0;
}
// Adds a value to the end of the chain
Queue.prototype.enqueue = function(value) {
// Check to see if value is defined
if (value) {
this.storage[this.count] = value;
this.count++;
}
}
// Removes a value from the beginning of the chain
Queue.prototype.dequeue = function() {
// Check to see if queue is empty
if (this.count - this.lowestCount === 0) {
return undefined;
}
var result = this.storage[this.lowestCount];
delete this.storage[this.lowestCount];
this.lowestCount++;
return result;
}
// Returns the length of the queue
Queue.prototype.size = function() {
return this.count - this.lowestCount;
}