File tree Expand file tree Collapse file tree 4 files changed +56
-1
lines changed Expand file tree Collapse file tree 4 files changed +56
-1
lines changed Original file line number Diff line number Diff line change 1+ const Node = require ( './node' ) ;
2+
3+ class LinkedList {
4+ constructor ( ) {
5+ this . root = null ;
6+ }
7+
8+ addLast ( value ) {
9+ const node = new Node ( value ) ;
10+
11+ if ( this . root ) {
12+ let currentNode = this . root ;
13+ while ( currentNode && currentNode . next ) {
14+ currentNode = currentNode . next ;
15+ }
16+ currentNode . next = node ;
17+ } else {
18+ this . root = node ;
19+ }
20+ }
21+ }
22+
23+ module . exports = LinkedList ;
Original file line number Diff line number Diff line change 1+ const LinkedList = require ( './linked-list' ) ;
2+
3+ describe ( 'LinkedList' , function ( ) {
4+ let linkedList ;
5+
6+ beforeEach ( ( ) => {
7+ linkedList = new LinkedList ( ) ;
8+ } ) ;
9+
10+ describe ( '#addLast' , ( ) => {
11+ it ( 'should add element to end' , ( ) => {
12+ linkedList . addLast ( 'a' ) ;
13+ expect ( linkedList . root . value ) . toBe ( 'a' ) ;
14+ } ) ;
15+
16+ it ( 'should link values' , ( ) => {
17+ linkedList . addLast ( 'a' ) ;
18+ linkedList . addLast ( 'b' ) ;
19+
20+ expect ( linkedList . root . value ) . toBe ( 'a' ) ;
21+ expect ( linkedList . root . next . value ) . toBe ( 'b' ) ;
22+ } ) ;
23+ } ) ;
24+ } ) ;
Original file line number Diff line number Diff line change 1+ class Node {
2+ constructor ( value ) {
3+ this . value = value ;
4+ this . next = null ;
5+ }
6+ }
7+
8+ module . exports = Node ;
Original file line number Diff line number Diff line change 1- const Queue = require ( './queue-1 .js' ) ;
1+ const Queue = require ( './queue.js' ) ;
22
33describe ( 'Queue' , function ( ) {
44 let queue ;
You can’t perform that action at this time.
0 commit comments