File tree 2 files changed +88
-0
lines changed
src/_DataStructures_/Stack/2-stacks-using1-array
2 files changed +88
-0
lines changed Original file line number Diff line number Diff line change @@ -14,16 +14,19 @@ Collection of interview questions with Unit Tests. Problems includes Data Struct
14
14
### Data Structures
15
15
16
16
- [ Singly Linked List] ( src/_DataStructures_/LinkedList )
17
+
17
18
- [ N Element From Last] ( src/_DataStructures_/LinkedList/element-from-last )
18
19
- [ Middle Node] ( src/_DataStructures_/LinkedList/middle-node )
19
20
- [ Detect Loop] ( src/_DataStructures_/LinkedList/loop-in-list )
20
21
- [ Reverse Linked List] ( src/_DataStructures_/LinkedList/reverse-linked-list )
22
+
21
23
- [ Stack] ( src/_DataStructures_/Stack )
22
24
23
25
- [ Implement Queue Using Stack] ( src/_DataStructures_/Stack/immitate-queue-using-stack )
24
26
- [ Baseball Game] ( src/_DataStructures_/Stack/baseball-game )
25
27
- [ Minimum Stack] ( src/_DataStructures_/Stack/min-stack )
26
28
- [ Balanced Parenthesis] ( src/_DataStructures_/Stack/balanced-parenthesis )
29
+ - [ Implement 2 Stacks using Single Array] ( src/_DataStructures_/Stack/2-stacks-using1-array )
27
30
28
31
- [ Queue] ( src/_DataStructures_/Queue )
29
32
- [ Weave] ( src/_DataStructures_/Queue/weave )
Original file line number Diff line number Diff line change
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
+ */
You can’t perform that action at this time.
0 commit comments