Skip to content

Commit 17eebb1

Browse files
authored
Merge pull request knaxus#36 from knaxus/problems
Implementation of 2 stacks using 1 array
2 parents a9cc626 + 5156f8d commit 17eebb1

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
1414
### Data Structures
1515

1616
- [Singly Linked List](src/_DataStructures_/LinkedList)
17+
1718
- [N Element From Last](src/_DataStructures_/LinkedList/element-from-last)
1819
- [Middle Node](src/_DataStructures_/LinkedList/middle-node)
1920
- [Detect Loop](src/_DataStructures_/LinkedList/loop-in-list)
2021
- [Reverse Linked List](src/_DataStructures_/LinkedList/reverse-linked-list)
22+
2123
- [Stack](src/_DataStructures_/Stack)
2224

2325
- [Implement Queue Using Stack](src/_DataStructures_/Stack/immitate-queue-using-stack)
2426
- [Baseball Game](src/_DataStructures_/Stack/baseball-game)
2527
- [Minimum Stack](src/_DataStructures_/Stack/min-stack)
2628
- [Balanced Parenthesis](src/_DataStructures_/Stack/balanced-parenthesis)
29+
- [Implement 2 Stacks using Single Array](src/_DataStructures_/Stack/2-stacks-using1-array)
2730

2831
- [Queue](src/_DataStructures_/Queue)
2932
- [Weave](src/_DataStructures_/Queue/weave)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* Revision to PR #35 where I implemented bullshit thinking of
3+
* new breakthrough :D
4+
*/
5+
6+
class TwoStacks {
7+
constructor(capacity) {
8+
this.data = [];
9+
this.top1 = -1;
10+
this.top2 = capacity;
11+
this.overflow = new Error('Overflow: Stack is full');
12+
13+
this.capacity = capacity;
14+
}
15+
16+
push1(value) {
17+
if (this.top1 < this.top2 - 1) {
18+
this.top1 += 1;
19+
this.data[this.top1] = value;
20+
} else {
21+
throw this.overflow;
22+
}
23+
}
24+
25+
push2(value) {
26+
if (this.top1 < this.top2 - 1) {
27+
this.top2 -= 1;
28+
this.data[this.top2] = value;
29+
} else {
30+
throw this.overflow;
31+
}
32+
}
33+
34+
pop1() {
35+
if (this.top1 >= 0) {
36+
const item = this.data[this.top1];
37+
delete this.data[this.top1];
38+
this.top1 -= 1;
39+
return item;
40+
}
41+
return -1;
42+
}
43+
44+
pop2() {
45+
if (this.top2 < this.capacity) {
46+
const item = this.data[this.top2];
47+
delete this.data[this.top2];
48+
this.top2 += 1;
49+
return item;
50+
}
51+
return -1;
52+
}
53+
}
54+
55+
module.exports = TwoStacks;
56+
57+
/** Test cases */
58+
59+
/*
60+
const s = new TwoStacks(4);
61+
62+
s.push1('a');
63+
console.log(s.data);
64+
65+
s.push2('a2');
66+
console.log(s.data);
67+
68+
s.push1('b');
69+
console.log(s.data);
70+
71+
s.push2('b2');
72+
console.log(s.data);
73+
74+
s.push2('d2');
75+
console.log(s.data);
76+
77+
s.push2('c23');
78+
console.log(s.data);
79+
80+
console.log(s.pop2());
81+
console.log(s.data);
82+
83+
console.log(s.pop1());
84+
console.log(s.data);
85+
*/

0 commit comments

Comments
 (0)