1- var SetOfStacks = function ( capacity ) {
2- // implement as an array of stacks
3- this . capacity = capacity ;
4- this . stackSet = [ ] ;
5- } ;
1+ // implement as array of stacks
2+ const Stack = require ( "../util/Stack" ) ;
63
7- SetOfStacks . prototype . push = function ( value ) {
8- if ( this . stackSet . length === 0 || this . stackSet [ this . stackSet . length - 1 ] . length === this . capacity ) {
9- var newStack = [ ] ;
10- newStack . push ( value ) ;
11- this . stackSet . push ( newStack ) ;
12- } else {
13- this . stackSet [ this . stackSet . length - 1 ] . push ( value ) ;
4+ class SetOfStacks {
5+ constructor ( capacity ) {
6+ this . capacity = capacity ;
7+ this . stackSet = [ ] ;
148 }
15- } ;
169
17- SetOfStacks . prototype . pop = function ( ) {
18- if ( this . numStack === 0 ) {
19- return undefined ;
20- } else if ( this . stackSet [ this . stackSet . length - 1 ] . length === 0 ) {
21- this . stackSet . pop ( ) ;
22- }
23- return this . stackSet [ this . stackSet . length - 1 ] . pop ( ) ;
24- } ;
10+ getLastStack ( ) {
11+ return this . stackSet [ this . stackSet . length - 1 ] ;
12+ }
13+
14+ push ( value ) {
15+ let last = this . getLastStack ( ) ;
16+ if ( this . stackSet . length === 0 || last . size ( ) === this . capacity ) {
17+ var newStack = new Stack ( ) ;
18+ newStack . push ( value ) ;
19+ this . stackSet . push ( newStack ) ;
20+ } else {
21+ last . push ( value ) ;
22+ }
23+ }
2524
26- SetOfStacks . prototype . peek = function ( ) {
27- var currStack = this . stackSet [ this . stackSet . length - 1 ] ;
28- return currStack [ currStack . length - 1 ] ;
29- } ;
25+ pop ( ) {
26+ if ( this . stackSet . length === 0 ) {
27+ return undefined ;
28+ }
29+ let last = this . getLastStack ( ) ;
30+ let value = last . pop ( ) ;
31+ if ( last . size ( ) === 0 ) {
32+ this . stackSet . pop ( ) ;
33+ }
34+ return value ;
35+ }
36+
37+ peek ( ) {
38+ let last = this . getLastStack ( ) ;
39+ return last . peek ( ) ;
40+ }
3041
31- SetOfStacks . prototype . isEmpty = function ( ) {
32- return this . stackSet . length === 0 ;
33- } ;
42+ isEmpty ( ) {
43+ return this . stackSet . length === 0 ;
44+ }
3445
35- SetOfStacks . prototype . popAt = function ( index ) {
36- return this . stackSet [ index ] . pop ( ) ;
37- } ;
46+ popAt ( index ) {
47+ // out of range index
48+ if ( index < 0 || index >= this . stackSet . length ) return false ;
49+ let value = this . stackSet [ index ] . pop ( ) ;
50+ if ( this . stackSet [ index ] . size ( ) == 0 ) {
51+ // clear the stack from the set
52+ this . stackSet . splice ( index , 1 ) ;
53+ }
54+ return value ;
55+ }
56+ }
3857
3958/* TESTS */
4059
@@ -54,11 +73,13 @@ s.push(12);
5473s . push ( 13 ) ;
5574s . push ( 14 ) ;
5675
57- console . log ( s . stackSet ) ;
76+ console . log ( s . peek ( ) , 14 ) ;
5877
78+ s . popAt ( 2 ) ;
79+ s . popAt ( 2 ) ;
5980s . popAt ( 2 ) ;
6081
61- console . log ( s . stackSet ) ;
82+ console . log ( s . peek ( ) , 14 ) ;
6283
6384s . pop ( ) ;
6485s . pop ( ) ;
@@ -70,8 +91,4 @@ s.pop();
7091s . pop ( ) ;
7192s . pop ( ) ;
7293
73- console . log ( s . stackSet ) ;
74-
75-
76- // Note: if stack not implemented as an array, would need to separately keep track of the depth
77- // of each stack in an array
94+ console . log ( s . peek ( ) , 2 ) ;
0 commit comments