File tree Expand file tree Collapse file tree 10 files changed +117
-19
lines changed Expand file tree Collapse file tree 10 files changed +117
-19
lines changed Original file line number Diff line number Diff line change @@ -21,4 +21,4 @@ class Bag {
2121}
2222}
2323
24- export default Bag
24+ module . exports = Bag
Original file line number Diff line number Diff line change 1+ const assert = require ( "assert" )
2+ const Bag = require ( "./Bag.js" )
3+
4+ const bag = new Bag ( )
5+
6+ bag . add ( 1 )
7+
8+ assert . deepEqual ( bag . size ( ) , 1 )
9+
10+ for ( const item of bag ) {
11+ assert . deepEqual ( item , 1 )
12+ }
Original file line number Diff line number Diff line change @@ -79,4 +79,4 @@ class LinkedList {
7979}
8080}
8181
82- export default LinkedList
82+ module . exports = LinkedList
Original file line number Diff line number Diff line change 1+ const assert = require ( "assert" )
2+ const LinkedList = require ( "./LinkedList.js" )
3+
4+ const linkedList = new LinkedList ( )
5+
6+ linkedList . push ( 1 )
7+ linkedList . push ( 2 )
8+
9+ assert . deepEqual ( linkedList . isEmpty ( ) , false )
10+ assert . deepEqual ( linkedList . size ( ) , 2 )
11+ assert . deepEqual ( linkedList . pop ( ) , 2 )
12+ assert . deepEqual ( linkedList . pop ( ) , 1 )
13+ assert . deepEqual ( linkedList . isEmpty ( ) , true )
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ class FIFOQueue {
88}
99
1010enqueue ( item ) {
11- this . #items. add ( item )
11+ this . #items. push ( item )
1212
1313this . #itemsCount++
1414}
@@ -48,4 +48,4 @@ class FIFOQueue {
4848}
4949}
5050
51- export default FIFOQueue
51+ module . exports = FIFOQueue
Original file line number Diff line number Diff line change 1+ const assert = require ( "assert" )
2+ const FIFOQueue = require ( "./FIFOQueue.js" )
3+
4+ const fifoQueue = new FIFOQueue ( )
5+
6+ fifoQueue . enqueue ( 1 )
7+ fifoQueue . enqueue ( 2 )
8+
9+ assert . deepEqual ( fifoQueue . isEmpty ( ) , false )
10+ assert . deepEqual ( fifoQueue . size ( ) , 2 )
11+ assert . deepEqual ( fifoQueue . dequeue ( ) , 1 )
12+ assert . deepEqual ( fifoQueue . dequeue ( ) , 2 )
13+ assert . deepEqual ( fifoQueue . isEmpty ( ) , true )
Original file line number Diff line number Diff line change 1- import PushdownStack from "./PushdownStack"
2-
3- class FixedCapacityStack extends PushdownStack {
1+ class FixedCapacityStack {
42#maxCapacity
3+ #items
4+ #itemsCount
55
66constructor ( maxCapacity ) {
7- super ( )
8-
97this . #maxCapacity = maxCapacity
108
11- this . items = new Array ( this . #maxCapacity)
9+ this . #itemsCount = 0
10+
11+ this . #items = new Array ( this . #maxCapacity)
1212}
1313
1414push ( item ) {
@@ -20,16 +20,50 @@ class FixedCapacityStack extends PushdownStack {
2020)
2121}
2222
23- this . #items. add ( item )
23+ this . #items. push ( item )
2424
2525this . #itemsCount++
2626}
2727
28+ pop ( ) {
29+ const isEmpty = this . isEmpty ( )
30+
31+ if ( isEmpty ) {
32+ throw new Error ( "This stack is empty!" )
33+ }
34+
35+ const leastRecentItem = this . #items. pop ( )
36+
37+ this . #itemsCount--
38+
39+ return leastRecentItem
40+ }
41+
42+ size ( ) {
43+ const size = this . #itemsCount
44+
45+ return size
46+ }
47+
48+ isEmpty ( ) {
49+ const isEmpty = this . #itemsCount === 0
50+
51+ return isEmpty
52+ }
53+
2854isFull ( ) {
29- const isFull = this . #maxCapacity >= this . size ( )
55+ const isFull = this . size ( ) >= this . #maxCapacity
3056
3157return isFull
3258}
59+
60+ * [ Symbol . iterator ] ( ) {
61+ for ( let i = 0 ; i < this . size ( ) ; i ++ ) {
62+ const item = this . pop ( )
63+
64+ yield item
65+ }
66+ }
3367}
3468
35- export default FixedCapacityStack
69+ module . exports = FixedCapacityStack
Original file line number Diff line number Diff line change 1+ const assert = require ( "assert" )
2+ const FixedCapacityStack = require ( "./FixedCapacityStack.js" )
3+
4+ const fixedCapacityStack = new FixedCapacityStack ( 2 )
5+
6+ fixedCapacityStack . push ( 1 )
7+ fixedCapacityStack . push ( 2 )
8+
9+ assert . deepEqual ( fixedCapacityStack . isEmpty ( ) , false )
10+ assert . deepEqual ( fixedCapacityStack . isFull ( ) , true )
11+ assert . deepEqual ( fixedCapacityStack . size ( ) , 2 )
12+ assert . throws ( ( ) => fixedCapacityStack . push ( 3 ) )
13+ assert . deepEqual ( fixedCapacityStack . pop ( ) , 2 )
14+ assert . deepEqual ( fixedCapacityStack . pop ( ) , 1 )
15+ assert . throws ( ( ) => fixedCapacityStack . pop ( ) )
16+ assert . deepEqual ( fixedCapacityStack . isEmpty ( ) , true )
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ class PushdownStack {
88}
99
1010push ( item ) {
11- this . #items. add ( item )
11+ this . #items. push ( item )
1212
1313this . #itemsCount++
1414}
@@ -46,10 +46,6 @@ class PushdownStack {
4646yield item
4747}
4848}
49-
50- set items ( items ) {
51- this . #items = items
52- }
5349}
5450
55- export default PushdownStack
51+ module . exports = PushdownStack
Original file line number Diff line number Diff line change 1+ const assert = require ( "assert" )
2+ const PushdownStack = require ( "./PushdownStack.js" )
3+
4+ const pushdownStack = new PushdownStack ( )
5+
6+ pushdownStack . push ( 1 )
7+ pushdownStack . push ( 2 )
8+
9+ assert . deepEqual ( pushdownStack . isEmpty ( ) , false )
10+ assert . deepEqual ( pushdownStack . size ( ) , 2 )
11+ assert . deepEqual ( pushdownStack . pop ( ) , 2 )
12+ assert . deepEqual ( pushdownStack . pop ( ) , 1 )
13+ assert . throws ( ( ) => pushdownStack . pop ( ) )
14+ assert . deepEqual ( pushdownStack . isEmpty ( ) , true )
You can’t perform that action at this time.
0 commit comments