@@ -32,6 +32,7 @@ import Algorithm, {
32
32
addLabelToAlgorithmBar ,
33
33
} from './Algorithm' ;
34
34
import { act } from '../anim/AnimationMain' ;
35
+ import pseudocodeText from '../pseudocode.json'
35
36
36
37
const INFO_MSG_X = 25 ;
37
38
const INFO_MSG_Y = 15 ;
@@ -66,13 +67,17 @@ const TAIL_LABEL_Y = 500;
66
67
const HEAD_ELEM_WIDTH = 30 ;
67
68
const HEAD_ELEM_HEIGHT = 30 ;
68
69
70
+ const CODE_START_X = 135 ;
71
+ const CODE_START_Y = 230 ;
72
+
69
73
const SIZE = 32 ;
70
74
71
75
export default class LinkedList extends Algorithm {
72
76
constructor ( am , w , h ) {
73
77
super ( am , w , h ) ;
74
78
this . addControls ( ) ;
75
79
this . nextIndex = 0 ;
80
+ this . commands = [ ] ;
76
81
this . setup ( ) ;
77
82
this . initialIndex = this . nextIndex ;
78
83
}
@@ -222,9 +227,9 @@ export default class LinkedList extends Algorithm {
222
227
this . tailEnabled = ! this . tailEnabled ;
223
228
this . implementAction ( this . clearAll . bind ( this ) ) ;
224
229
if ( this . tailEnabled ) {
225
- this . animationManager . setAllLayers ( [ 0 , 1 ] ) ;
230
+ this . animationManager . setAllLayers ( [ 0 , 1 , 32 ] ) ; // To make pseudocode display by default you need to add "32" to this array
226
231
} else {
227
- this . animationManager . setAllLayers ( [ 0 ] ) ;
232
+ this . animationManager . setAllLayers ( [ 0 , 32 ] ) ;
228
233
}
229
234
}
230
235
@@ -286,11 +291,16 @@ export default class LinkedList extends Algorithm {
286
291
this . cmd ( act . setLayer , this . tailID , 1 ) ;
287
292
288
293
if ( this . tailEnabled ) {
289
- this . animationManager . setAllLayers ( [ 0 , 1 ] ) ;
294
+ this . animationManager . setAllLayers ( [ 0 , 1 , 32 ] ) ;
290
295
} else {
291
- this . animationManager . setAllLayers ( [ 0 ] ) ;
296
+ this . animationManager . setAllLayers ( [ 0 , 32 ] ) ;
292
297
}
293
298
299
+ // Pseudocode
300
+ this . pseudocode = pseudocodeText . SinglyLinkedList ;
301
+
302
+ this . resetIndex = this . nextIndex ;
303
+
294
304
this . animationManager . startNewAnimation ( this . commands ) ;
295
305
this . animationManager . skipForward ( ) ;
296
306
this . animationManager . clearHistory ( ) ;
@@ -319,7 +329,7 @@ export default class LinkedList extends Algorithm {
319
329
if ( index >= 0 && index <= this . size ) {
320
330
this . addValueField . value = '' ;
321
331
this . addIndexField . value = '' ;
322
- this . implementAction ( this . add . bind ( this ) , addVal , index ) ;
332
+ this . implementAction ( this . add . bind ( this ) , addVal , index , false , false , true , false ) ;
323
333
} else {
324
334
this . implementAction (
325
335
this . setInfoText . bind ( this ) ,
@@ -339,7 +349,7 @@ export default class LinkedList extends Algorithm {
339
349
if ( this . addValueField . value !== '' ) {
340
350
const addVal = parseInt ( this . addValueField . value ) ;
341
351
this . addValueField . value = '' ;
342
- this . implementAction ( this . add . bind ( this ) , addVal , 0 ) ;
352
+ this . implementAction ( this . add . bind ( this ) , addVal , 0 , true , false , false , false ) ;
343
353
} else {
344
354
this . implementAction ( this . setInfoText . bind ( this ) , 'Missing input data.' ) ;
345
355
this . shake ( this . addFrontButton ) ;
@@ -350,7 +360,7 @@ export default class LinkedList extends Algorithm {
350
360
if ( this . addValueField . value !== '' ) {
351
361
const addVal = parseInt ( this . addValueField . value ) ;
352
362
this . addValueField . value = '' ;
353
- this . implementAction ( this . add . bind ( this ) , addVal , this . size ) ;
363
+ this . implementAction ( this . add . bind ( this ) , addVal , this . size , false , true , false , false ) ;
354
364
} else {
355
365
this . implementAction ( this . setInfoText . bind ( this ) , 'Missing input data.' ) ;
356
366
this . shake ( this . addBackButton ) ;
@@ -362,7 +372,7 @@ export default class LinkedList extends Algorithm {
362
372
const index = this . removeField . value ;
363
373
if ( index >= 0 && index < this . size ) {
364
374
this . removeField . value = '' ;
365
- this . implementAction ( this . remove . bind ( this ) , index ) ;
375
+ this . implementAction ( this . remove . bind ( this ) , index , false , false , true ) ;
366
376
} else {
367
377
let errorMsg = 'Cannot remove from an empty list.' ;
368
378
if ( this . size === 1 ) {
@@ -381,7 +391,7 @@ export default class LinkedList extends Algorithm {
381
391
382
392
removeFrontCallback ( ) {
383
393
if ( this . size > 0 ) {
384
- this . implementAction ( this . remove . bind ( this ) , 0 ) ;
394
+ this . implementAction ( this . remove . bind ( this ) , 0 , true , false , false ) ;
385
395
} else {
386
396
this . implementAction ( this . setInfoText . bind ( this ) , 'Cannot remove from an empty list.' ) ;
387
397
this . shake ( this . removeFrontButton ) ;
@@ -390,7 +400,7 @@ export default class LinkedList extends Algorithm {
390
400
391
401
removeBackCallback ( ) {
392
402
if ( this . size > 0 ) {
393
- this . implementAction ( this . remove . bind ( this ) , this . size - 1 ) ;
403
+ this . implementAction ( this . remove . bind ( this ) , this . size - 1 , false , true , false ) ;
394
404
} else {
395
405
this . implementAction ( this . setInfoText . bind ( this ) , 'Cannot remove from an empty list.' ) ;
396
406
this . shake ( this . removeBackButton ) ;
@@ -413,7 +423,7 @@ export default class LinkedList extends Algorithm {
413
423
i -- ;
414
424
} else {
415
425
set . add ( val ) ;
416
- this . implementAction ( this . add . bind ( this ) , val , 0 ) ;
426
+ this . implementAction ( this . add . bind ( this ) , val , 0 , false , true , false , true ) ;
417
427
}
418
428
this . animationManager . skipForward ( ) ;
419
429
this . animationManager . clearHistory ( ) ;
@@ -424,24 +434,108 @@ export default class LinkedList extends Algorithm {
424
434
this . implementAction ( this . clearAll . bind ( this ) ) ;
425
435
}
426
436
427
- traverse ( startIndex , endIndex ) {
437
+ traverse ( startIndex , endIndex , runningAddBack ) {
438
+ this . unhighlight ( 4 , 0 , this . addBackCodeID ) ;
439
+ this . unhighlight ( 6 , 0 , this . addIndexCodeID ) ;
428
440
for ( let i = startIndex ; i <= endIndex ; i ++ ) {
441
+ this . unhighlight ( 6 , 0 , this . addBackCodeID ) ;
442
+ this . unhighlight ( 8 , 0 , this . addIndexCodeID ) ;
429
443
this . cmd ( act . step ) ;
444
+
430
445
this . cmd ( act . setHighlight , this . linkedListElemID [ i ] , 1 ) ;
431
446
if ( i > 0 ) {
432
447
this . cmd ( act . setHighlight , this . linkedListElemID [ i - 1 ] , 0 ) ;
433
448
}
449
+
450
+ if ( runningAddBack ) {
451
+ this . highlight ( 6 , 0 , this . addBackCodeID ) ;
452
+ } else {
453
+ this . highlight ( 8 , 0 , this . addIndexCodeID ) ;
454
+ }
455
+ this . cmd ( act . step ) ;
434
456
}
435
457
this . cmd ( act . step ) ;
436
458
}
437
459
438
- add ( elemToAdd , index ) {
460
+ add ( elemToAdd , index , isAddFront , isAddBack , isAddIndex , skipPseudocode ) {
439
461
this . commands = [ ] ;
440
462
this . setInfoText ( '' ) ;
441
463
464
+ if ( ! skipPseudocode ) {
465
+ this . addIndexCodeID = this . addCodeToCanvasBaseAll (
466
+ this . pseudocode ,
467
+ 'addIndex' ,
468
+ CODE_START_X ,
469
+ CODE_START_Y ,
470
+ ) ;
471
+ this . addFrontCodeID = this . addCodeToCanvasBaseAll (
472
+ this . pseudocode ,
473
+ 'addFront' ,
474
+ CODE_START_X + 370 ,
475
+ CODE_START_Y ,
476
+ ) ;
477
+ this . addBackCodeID = this . addCodeToCanvasBaseAll (
478
+ this . pseudocode ,
479
+ 'addBack' ,
480
+ CODE_START_X + 640 ,
481
+ CODE_START_Y ,
482
+ ) ;
483
+ }
484
+
485
+ if ( isAddFront ) {
486
+ this . highlight ( 0 , 0 , this . addFrontCodeID ) ;
487
+ } else if ( isAddBack ) {
488
+ this . highlight ( 0 , 0 , this . addBackCodeID ) ;
489
+ } else if ( isAddIndex ) {
490
+ this . highlight ( 0 , 0 , this . addIndexCodeID ) ;
491
+ }
492
+
493
+ const runningAddFront = isAddFront || ( isAddIndex && index === 0 ) ; // addfront is called directly or addindex
494
+ const runningAddBack = isAddBack || ( isAddIndex && index === this . size && ! runningAddFront ) ; // addback is called directly or addindex
495
+ const runningAddIndexOnly = ! runningAddFront && ! runningAddBack ; // addindex is called directly
496
+
497
+ if ( isAddIndex ) {
498
+ if ( runningAddFront ) {
499
+ this . cmd ( act . step ) ;
500
+ this . highlight ( 1 , 0 , this . addIndexCodeID ) ;
501
+ this . highlight ( 2 , 0 , this . addIndexCodeID ) ;
502
+ this . highlight ( 0 , 0 , this . addFrontCodeID ) ;
503
+ } else if ( runningAddBack ) {
504
+ this . cmd ( act . step ) ;
505
+ this . highlight ( 3 , 0 , this . addIndexCodeID ) ;
506
+ this . highlight ( 4 , 0 , this . addIndexCodeID ) ;
507
+ this . highlight ( 0 , 0 , this . addBackCodeID ) ;
508
+ }
509
+ }
510
+
511
+ this . cmd ( act . step ) ;
512
+
442
513
const labPushID = this . nextIndex ++ ;
443
514
const labPushValID = this . nextIndex ++ ;
444
515
516
+ if ( runningAddFront ) {
517
+ this . highlight ( 1 , 0 , this . addFrontCodeID ) ;
518
+ } else if ( runningAddBack ) {
519
+ this . highlight ( 1 , 0 , this . addBackCodeID ) ;
520
+ } else {
521
+ this . highlight ( 5 , 0 , this . addIndexCodeID ) ;
522
+ }
523
+
524
+ this . cmd ( act . step ) ;
525
+
526
+ if ( runningAddBack && this . size > 0 ) {
527
+ this . unhighlight ( 1 , 0 , this . addBackCodeID ) ;
528
+ this . highlight ( 3 , 0 , this . addBackCodeID ) ;
529
+ this . cmd ( act . step ) ;
530
+ this . highlight ( 4 , 0 , this . addBackCodeID ) ;
531
+ this . cmd ( act . step ) ;
532
+ this . unhighlight ( 4 , 0 , this . addBackCodeID ) ;
533
+ this . highlight ( 5 , 0 , this . addBackCodeID ) ;
534
+ } else if ( runningAddIndexOnly ) {
535
+ this . highlight ( 6 , 0 , this . addIndexCodeID ) ;
536
+ this . cmd ( act . step ) ;
537
+ this . highlight ( 7 , 0 , this . addIndexCodeID ) ;
538
+ }
445
539
for ( let i = this . size - 1 ; i >= index ; i -- ) {
446
540
this . arrayData [ i + 1 ] = this . arrayData [ i ] ;
447
541
this . linkedListElemID [ i + 1 ] = this . linkedListElemID [ i ] ;
@@ -452,9 +546,9 @@ export default class LinkedList extends Algorithm {
452
546
this . cmd ( act . setText , this . leftoverLabelID , '' ) ;
453
547
454
548
if ( this . tailEnabled && index === this . size ) {
455
- this . traverse ( this . size - 1 , this . size ) ;
549
+ this . traverse ( this . size - 1 , this . size , runningAddBack ) ;
456
550
} else {
457
- this . traverse ( 0 , index - 1 ) ;
551
+ this . traverse ( 0 , index - 1 , runningAddBack ) ;
458
552
}
459
553
460
554
this . cmd (
@@ -473,6 +567,29 @@ export default class LinkedList extends Algorithm {
473
567
this . cmd ( act . createLabel , labPushID , 'Adding Value: ' , PUSH_LABEL_X , PUSH_LABEL_Y ) ;
474
568
this . cmd ( act . createLabel , labPushValID , elemToAdd , PUSH_ELEMENT_X , PUSH_ELEMENT_Y ) ;
475
569
570
+ if ( runningAddFront ) {
571
+ if ( this . size === 0 ) {
572
+ this . highlight ( 2 , 0 , this . addFrontCodeID ) ;
573
+ } else {
574
+ this . unhighlight ( 1 , 0 , this . addFrontCodeID ) ;
575
+ this . highlight ( 3 , 0 , this . addFrontCodeID ) ;
576
+ this . cmd ( act . step ) ;
577
+ this . highlight ( 4 , 0 , this . addFrontCodeID ) ;
578
+ this . highlight ( 5 , 0 , this . addFrontCodeID ) ;
579
+ this . highlight ( 6 , 0 , this . addFrontCodeID ) ;
580
+ }
581
+ } else if ( runningAddBack ) {
582
+ if ( this . size === 0 ) {
583
+ this . highlight ( 2 , 0 , this . addBackCodeID ) ;
584
+ }
585
+ } else {
586
+ this . unhighlight ( 7 , 0 , this . addIndexCodeID ) ;
587
+ this . unhighlight ( 8 , 0 , this . addIndexCodeID ) ;
588
+ this . highlight ( 10 , 0 , this . addIndexCodeID ) ;
589
+ this . highlight ( 11 , 0 , this . addIndexCodeID ) ;
590
+ this . highlight ( 12 , 0 , this . addIndexCodeID ) ;
591
+ }
592
+
476
593
this . cmd ( act . step ) ;
477
594
478
595
this . cmd ( act . move , labPushValID , LINKED_LIST_INSERT_X , LINKED_LIST_INSERT_Y ) ;
@@ -504,6 +621,11 @@ export default class LinkedList extends Algorithm {
504
621
this . linkedListElemID [ index - 1 ] ,
505
622
this . linkedListElemID [ index ] ,
506
623
) ;
624
+ if ( runningAddBack ) {
625
+ this . unhighlight ( 5 , 0 , this . addBackCodeID ) ;
626
+ this . unhighlight ( 6 , 0 , this . addBackCodeID ) ;
627
+ this . highlight ( 7 , 0 , this . addBackCodeID ) ;
628
+ }
507
629
} else {
508
630
this . cmd (
509
631
act . disconnect ,
@@ -527,20 +649,67 @@ export default class LinkedList extends Algorithm {
527
649
}
528
650
529
651
this . cmd ( act . setHighlight , this . linkedListElemID [ index - 1 ] , 0 ) ;
530
-
531
652
this . cmd ( act . step ) ;
653
+
654
+ if ( runningAddFront ) {
655
+ this . unhighlight ( 1 , 0 , this . addFrontCodeID ) ;
656
+ this . unhighlight ( 2 , 0 , this . addFrontCodeID ) ;
657
+ this . unhighlight ( 3 , 0 , this . addFrontCodeID ) ;
658
+ this . unhighlight ( 4 , 0 , this . addFrontCodeID ) ;
659
+ this . unhighlight ( 5 , 0 , this . addFrontCodeID ) ;
660
+ this . unhighlight ( 6 , 0 , this . addFrontCodeID ) ;
661
+ this . highlight ( 8 , 0 , this . addFrontCodeID ) ;
662
+ } else if ( runningAddBack ) {
663
+ this . unhighlight ( 1 , 0 , this . addBackCodeID ) ;
664
+ this . unhighlight ( 2 , 0 , this . addBackCodeID ) ;
665
+ this . unhighlight ( 3 , 0 , this . addBackCodeID ) ;
666
+ this . unhighlight ( 7 , 0 , this . addBackCodeID ) ;
667
+ this . highlight ( 9 , 0 , this . addBackCodeID ) ;
668
+ } else {
669
+ this . unhighlight ( 8 , 0 , this . addIndexCodeID ) ;
670
+ this . unhighlight ( 10 , 0 , this . addIndexCodeID ) ;
671
+ this . unhighlight ( 11 , 0 , this . addIndexCodeID ) ;
672
+ this . unhighlight ( 12 , 0 , this . addIndexCodeID ) ;
673
+ this . highlight ( 13 , 0 , this . addIndexCodeID ) ;
674
+ }
675
+
532
676
this . size = this . size + 1 ;
533
677
this . resetNodePositions ( ) ;
534
678
this . cmd ( act . delete , labPushID ) ;
535
679
this . cmd ( act . step ) ;
536
680
681
+ if ( ! skipPseudocode ) {
682
+ this . removeCode ( this . addFrontCodeID ) ;
683
+ this . removeCode ( this . addBackCodeID ) ;
684
+ this . removeCode ( this . addIndexCodeID ) ;
685
+ }
686
+
537
687
return this . commands ;
538
688
}
539
689
540
- remove ( index ) {
690
+ remove ( index , isRemoveFront , isRemoveBack , isRemoveIndex ) {
541
691
this . commands = [ ] ;
542
692
this . setInfoText ( '' ) ;
543
693
694
+ this . removeIndexCodeID = this . addCodeToCanvasBaseAll (
695
+ this . pseudocode ,
696
+ 'removeIndex' ,
697
+ CODE_START_X ,
698
+ CODE_START_Y ,
699
+ ) ;
700
+ this . removeFrontCodeID = this . addCodeToCanvasBaseAll (
701
+ this . pseudocode ,
702
+ 'removeFront' ,
703
+ CODE_START_X + 325 ,
704
+ CODE_START_Y ,
705
+ ) ;
706
+ this . removeBackCodeID = this . addCodeToCanvasBaseAll (
707
+ this . pseudocode ,
708
+ 'removeBack' ,
709
+ CODE_START_X + 620 ,
710
+ CODE_START_Y ,
711
+ ) ;
712
+
544
713
index = parseInt ( index ) ;
545
714
const labPopID = this . nextIndex ++ ;
546
715
const labPopValID = this . nextIndex ++ ;
0 commit comments