-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
34 lines (33 loc) · 938 Bytes
/
index.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
module.exports = queue;
queue(module.exports);
function queue(mixin) {
var stack1 = [];
var stack2 = [];
var exports = mixin || {};
exports.enqueue = function (item) {
stack1.push(item);
};
function repare() {
if (stack2.length === 0) {
var length = stack1.length;
for (var i = 0; i < length; i++) {
stack2.push(stack1.pop());
}
}
return exports;
}
exports.dequeue = function () {
repare();
if (!stack2.length) throw new Error('Can\'t dequeue from an empty queue');
return stack2.pop();
};
exports.peek = function () {
repare();
if (!stack2.length) throw new Error('Can\'t peek from an empty queue');
return stack2[stack2.length - 1];
};
exports.isEmpty = function () {
return stack1.length === 0 && stack2.length === 0;
};
return exports;
}