-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtriple-stack.js
More file actions
61 lines (54 loc) · 1.51 KB
/
triple-stack.js
File metadata and controls
61 lines (54 loc) · 1.51 KB
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
"use strict";
/**
* TripleStack class holds 3 stacks in one array. This is done by interleaving
* the values from the 3 indexes, so the first items are at 0, 1 and 2 and
* subsequent items are every 3 places from those. This class takes advantage
* of the fact that JavaScript arrays are dynamic and doesn't hold the stacks
* to any size. It doesn't reduce the size of the underlying array when items
* are popped but that could easily be added.
*
* Time: push O(1), pop O(1), peek O(1)
* Additional space: push O(1), pop O(1), peek O(1)
*/
export class TripleStack {
constructor() {
this._array = [];
this._lengths = [0, 0, 0];
}
_getLength(stack) {
return this._lengths[stack - 1];
}
push(stack, value) {
let idx = this._getLength(stack) * 3 + stack - 1;
this._array[idx] = value;
++this._lengths[stack - 1];
}
pop(stack) {
let length = this._getLength(stack),
value;
if (length > 0) {
let idx = (length - 1) * 3 + stack - 1;
value = this._array[idx];
this._array[idx] = undefined;
--this._lengths[stack - 1];
}
return value;
}
peek(stack) {
let length = this._getLength(stack),
value;
if (length > 0) {
let idx = (length - 1) * 3 + stack - 1;
value = this._array[idx];
}
return value;
}
isEmpty(stack) {
return this._getLength(stack) === 0;
}
}
const myTripleStack = new TripleStack();
myTripleStack.push(3, 3);
myTripleStack.push(3, 3);
myTripleStack.push(3, 3);
myTripleStack.push(1, 1);