-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreeInOne.js
48 lines (38 loc) · 1.06 KB
/
threeInOne.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
/** Three in One: Describe how you could use a single array to implement three stacks. */
class MultiStack {
constructor(noOfStacks, stackSize) {
this.capacity = stackSize;
this.sizes = new Array(noOfStacks).fill(0);
this.storage = [];
}
isFull(stackNo) {
return this.sizes[stackNo] == this.capacity ? true : false;
}
isEmpty(stackNo) {
return this.sizes[stackNo] == 0 ? true : false;
}
push(stackNo, value) {
if (this.isFull(stackNo)) return console.log("Stack Overflow");
this.sizes[stackNo]++;
this.storage[this.indexOfTop(stackNo)] = value;
}
pop(stackNo) {
if (this.isEmpty(stackNo)) return console.log("Stack is Empty");
this.storage[this.indexOfTop(stackNo)] = 0;
this.sizes[stackNo]--;
}
indexOfTop(stackNo) {
let offset = stackNo * this.capacity;
let size = this.sizes[stackNo];
return offset + size - 1;
}
}
let stack = new MultiStack(3, 3);
stack.push(0, 1);
stack.push(0, 2);
stack.push(0, 3);
stack.push(2, 7);
stack.push(2, 8);
stack.pop(2);
stack.push(2, 9);
console.log(stack);