-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathStack.js
80 lines (75 loc) · 2.01 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* Stack class.
*
* @author KhaaZ
*
* @template T
*
* @prop {Array<T>} _elements
* @prop {Boolean} max - Maximum number of elements that can be added in this Stack
* @prop {Boolean} replaceOnMax - Whether to replace value when adding if max is reached (unstack first element and stack new element)
*
* @class Stack
*/
class Stack {
/**
* Creates an instance of Stack.
*
* @param {Number} [max=null] - Maximum number of elements that can be added in this Stack
* @param {Boolean} [replaceOnMax=true] - Whether to replace value when adding if max is reached (unstack first element and stack new element)
* @memberof Stack
*/
constructor(max = null, replaceOnMax = true) {
this._elements = [];
this.max = max;
this.replaceOnMax = replaceOnMax;
}
/**
* Returns the Stack size
*
* @readonly
* @type {Number}
* @memberof Stack
*/
get size() {
return this._elements.length;
}
/**
* Return first element of this Stack (top of the Stack).
*
* @returns {T}
* @memberof Stack
*/
first() {
return this._elements[this._elements.length - 1];
}
/**
* Stack up an element.
*
* @param {T} elem
* @param {Boolean} [replaceOnMax] - Whether to replace value when adding if max is reached (unstack first element and stack new element)
* @returns {Boolean} Whether element was successfully added
* @memberof Stack
*/
stack(elem, replaceOnMax) {
if (this.max && this._elements.length === this.max) {
if ( (replaceOnMax !== undefined) ? replaceOnMax : this.replaceOnMax) {
this._elements.pop();
} else {
return false;
}
}
this._elements.push(elem);
return true;
}
/**
* Unstack an element and returns it
*
* @returns {T}
* @memberof Stack
*/
unstack() {
return this._elements.pop();
}
}
export default Stack;