-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
61 lines (53 loc) · 1.37 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
//implementing queue using objects
var Queue = function (capacity) {
this._count = 0;
this._storage = {};
this._capacity = capacity;
}
Queue.prototype.enqueue = function (value) {
if(this._capacity === this._count) {
return "Max capacity reached";
}
this._storage[this._count] = value;
return ++this._count;
}
Queue.prototype.dequeue = function () {
if(this._count === 0) {
return false;
}
var self = this;
var value = this._storage[0];
Object.keys(this._storage).forEach(function(index) {
self._storage[+index] = self._storage[+index + 1];
});
delete this._storage[this._count - 1];
this._count--;
return value;
}
Queue.prototype.peek = function () {
return this._storage[this._count - 1];
}
Queue.prototype.count = function () {
return this._count;
}
Queue.prototype.contains = function (toFind) {
var self = this;
var _found = false;
Object.keys(this._storage).forEach(function (index) {
if(self._storage[index] == toFind) {
_found = true;
}
});
return _found;
}
Queue.prototype.until = function (toFind) {
var self = this;
var _foundAt = -1;
Object.keys(this._storage).forEach(function (index) {
if(self._storage[index] == toFind) {
_foundAt = +index + 1;
}
});
return _foundAt;
}
var q1 = new Queue();