@@ -9,20 +9,21 @@ describe('LinkedList', () => {
9
9
it ( 'should append node to linked list' , ( ) => {
10
10
const linkedList = new LinkedList ( ) ;
11
11
12
- const node1 = linkedList . append ( 1 ) ;
13
- const node2 = linkedList . append ( 2 ) ;
12
+ const node1 = linkedList . append ( { value : 1 } ) ;
13
+ const node2 = linkedList . append ( { value : 2 , key : 'test' } ) ;
14
14
15
15
expect ( node1 . value ) . toBe ( 1 ) ;
16
16
expect ( node2 . value ) . toBe ( 2 ) ;
17
+ expect ( node2 . key ) . toBe ( 'test' ) ;
17
18
18
- expect ( linkedList . toString ( ) ) . toBe ( '1,2' ) ;
19
+ expect ( linkedList . toString ( ) ) . toBe ( '1,test: 2' ) ;
19
20
} ) ;
20
21
21
22
it ( 'should prepend node to linked list' , ( ) => {
22
23
const linkedList = new LinkedList ( ) ;
23
24
24
- const node1 = linkedList . append ( 1 ) ;
25
- const node2 = linkedList . prepend ( 2 ) ;
25
+ const node1 = linkedList . append ( { value : 1 } ) ;
26
+ const node2 = linkedList . prepend ( { value : 2 } ) ;
26
27
27
28
expect ( node1 . value ) . toBe ( 1 ) ;
28
29
expect ( node2 . value ) . toBe ( 2 ) ;
@@ -33,21 +34,53 @@ describe('LinkedList', () => {
33
34
it ( 'should delete node by value from linked list' , ( ) => {
34
35
const linkedList = new LinkedList ( ) ;
35
36
36
- linkedList . append ( 1 ) ;
37
- linkedList . append ( 2 ) ;
38
- linkedList . append ( 3 ) ;
39
- linkedList . append ( 3 ) ;
40
- linkedList . append ( 4 ) ;
41
- linkedList . append ( 5 ) ;
37
+ linkedList . append ( { value : 1 } ) ;
38
+ linkedList . append ( { value : 2 } ) ;
39
+ linkedList . append ( { value : 3 } ) ;
40
+ linkedList . append ( { value : 3 } ) ;
41
+ linkedList . append ( { value : 4 } ) ;
42
+ linkedList . append ( { value : 5 } ) ;
42
43
43
- const deletedNode = linkedList . delete ( 3 ) ;
44
+ const deletedNode = linkedList . deleteByValue ( 3 ) ;
44
45
expect ( deletedNode . value ) . toBe ( 3 ) ;
45
46
expect ( linkedList . toString ( ) ) . toBe ( '1,2,3,4,5' ) ;
46
47
47
- linkedList . delete ( 3 ) ;
48
+ linkedList . deleteByValue ( 3 ) ;
48
49
expect ( linkedList . toString ( ) ) . toBe ( '1,2,4,5' ) ;
49
50
50
- linkedList . delete ( 1 ) ;
51
+ linkedList . deleteByValue ( 1 ) ;
51
52
expect ( linkedList . toString ( ) ) . toBe ( '2,4,5' ) ;
52
53
} ) ;
54
+
55
+ it ( 'should delete node by key from linked list' , ( ) => {
56
+ const linkedList = new LinkedList ( ) ;
57
+
58
+ linkedList . append ( { value : 1 , key : 'test1' } ) ;
59
+ linkedList . append ( { value : 2 , key : 'test2' } ) ;
60
+ linkedList . append ( { value : 3 , key : 'test3' } ) ;
61
+
62
+ const deletedNode = linkedList . deleteByKey ( 'test2' ) ;
63
+ expect ( deletedNode . key ) . toBe ( 'test2' ) ;
64
+ expect ( linkedList . toString ( ) ) . toBe ( 'test1:1,test3:3' ) ;
65
+ } ) ;
66
+
67
+ it ( 'should append unique nodes' , ( ) => {
68
+ const linkedList = new LinkedList ( ) ;
69
+
70
+ linkedList . appendUnique ( { value : 1 , key : 'test1' } ) ;
71
+ linkedList . appendUnique ( { value : 2 , key : 'test2' } ) ;
72
+ linkedList . appendUnique ( { value : 3 , key : 'test2' } ) ;
73
+
74
+ expect ( linkedList . toString ( ) ) . toBe ( 'test1:1,test2:3' ) ;
75
+ } ) ;
76
+
77
+ it ( 'should find node by its key' , ( ) => {
78
+ const linkedList = new LinkedList ( ) ;
79
+
80
+ linkedList . appendUnique ( { value : 1 , key : 'test1' } ) ;
81
+ linkedList . appendUnique ( { value : 2 , key : 'test2' } ) ;
82
+ linkedList . appendUnique ( { value : 3 , key : 'test3' } ) ;
83
+
84
+ expect ( linkedList . findByKey ( 'test3' ) . toString ( ) ) . toBe ( 'test3:3' ) ;
85
+ } ) ;
53
86
} ) ;
0 commit comments