File tree 2 files changed +99
-0
lines changed
2 files changed +99
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Node {
2
+ constructor ( data ) {
3
+ this . data = data ;
4
+ this . next = null ;
5
+ }
6
+ }
7
+
8
+ class LinkedList {
9
+ constructor ( ) {
10
+ this . head = new Node ( "head" ) ;
11
+ }
12
+
13
+ find ( data ) {
14
+ let temp = this . head ;
15
+ while ( temp . data != data )
16
+ temp = temp . next ;
17
+ return temp ;
18
+ }
19
+
20
+ findPrev ( data ) {
21
+ let temp = this . head ;
22
+ while ( temp . next != null && temp . next . data != data )
23
+ temp = temp . next ;
24
+ return temp ;
25
+ }
26
+
27
+ printList ( ) {
28
+ let temp = this . head ;
29
+ if ( temp . next == null )
30
+ return "The Linked List is Empty" ;
31
+ let str = "" ;
32
+ while ( temp . next != null ) {
33
+ str += temp . next . data + " -> " ;
34
+ temp = temp . next ;
35
+ }
36
+ str += "NULL\n" ;
37
+ return str ;
38
+ }
39
+
40
+ insertAfter ( data , currData ) {
41
+ const newNode = new Node ( data ) ;
42
+ let curr = this . find ( currData ) ;
43
+ newNode . next = curr . next ;
44
+ curr . next = newNode ;
45
+ }
46
+
47
+ remove ( data ) {
48
+ const prev = this . findPrev ( data ) ;
49
+ if ( prev . next != null )
50
+ prev . next = prev . next . next ;
51
+ }
52
+ }
53
+
54
+ module . exports = LinkedList ;
Original file line number Diff line number Diff line change
1
+ const expect = require ( 'chai' ) . expect ;
2
+ const SinglyLinkedList = require ( '../../examples/datastructures/singly-linked-list' ) ;
3
+
4
+ describe ( '=> SINGLY LINKED LIST' , function ( ) {
5
+ let names ;
6
+ before ( function ( ) {
7
+ names = new SinglyLinkedList ( ) ;
8
+ } ) ;
9
+
10
+ it ( 'should create an Empty Linked List' , function ( done ) {
11
+ expect ( names . head . data ) . to . equal ( "head" ) ;
12
+ done ( ) ;
13
+ } ) ;
14
+
15
+ it ( 'should print an Empty Linked List' , function ( done ) {
16
+ expect ( names . printList ( ) ) . to . equal ( "The Linked List is Empty" ) ;
17
+ done ( ) ;
18
+ } ) ;
19
+
20
+ it ( 'should insert into a Linked List' , function ( done ) {
21
+ names . insertAfter ( 1 , "head" ) ;
22
+ names . insertAfter ( 2 , 1 ) ;
23
+ names . insertAfter ( 3 , 2 ) ;
24
+ names . insertAfter ( 4 , 3 ) ;
25
+ expect ( names . printList ( ) ) . to . equal ( "1 -> 2 -> 3 -> 4 -> NULL\n" ) ;
26
+ done ( ) ;
27
+ } ) ;
28
+
29
+ it ( 'should remove element from a Linked List' , function ( done ) {
30
+ names . remove ( 1 ) ;
31
+ expect ( names . printList ( ) ) . to . equal ( "2 -> 3 -> 4 -> NULL\n" ) ;
32
+ done ( ) ;
33
+ } ) ;
34
+
35
+ describe ( '=> clear' , function ( ) {
36
+ it ( 'should clear the Linked List' , function ( done ) {
37
+ names . remove ( 2 ) ;
38
+ names . remove ( 3 ) ;
39
+ names . remove ( 4 ) ;
40
+ expect ( names . printList ( ) ) . to . equal ( "The Linked List is Empty" ) ;
41
+ done ( ) ;
42
+ } ) ;
43
+ } ) ;
44
+
45
+ } ) ;
You can’t perform that action at this time.
0 commit comments