-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack.js
53 lines (41 loc) · 942 Bytes
/
stack.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
var _ = require('lodash');
module.exports = (function() {
function Stack() {
var array = [];
var used = false;
this.isEmpty = function isEmpty() {
return array.length === 0;
};
this.isUsed = function isUsed() {
return used;
};
this.isNotUsed = function isUnused() {
return !used;
};
this.push = function push(element) {
array.push(element);
used = true;
};
this.pop = function pop() {
if (array.length === 0) {
return undefined;
}
return array.pop();
};
this.peek = function peek() {
if (array.length === 0) {
return undefined;
}
return array[array.length - 1];
};
this.size = function size() {
return array.length;
};
this.getValues = function getValues() {
return _.cloneDeep(array);
};
}
return function createStack() {
return new Stack();
};
})();