@@ -9,20 +9,21 @@ describe('LinkedList', () => {
99 it ( 'should append node to linked list' , ( ) => {
1010 const linkedList = new LinkedList ( ) ;
1111
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' } ) ;
1414
1515 expect ( node1 . value ) . toBe ( 1 ) ;
1616 expect ( node2 . value ) . toBe ( 2 ) ;
17+ expect ( node2 . key ) . toBe ( 'test' ) ;
1718
18- expect ( linkedList . toString ( ) ) . toBe ( '1,2' ) ;
19+ expect ( linkedList . toString ( ) ) . toBe ( '1,test: 2' ) ;
1920 } ) ;
2021
2122 it ( 'should prepend node to linked list' , ( ) => {
2223 const linkedList = new LinkedList ( ) ;
2324
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 } ) ;
2627
2728 expect ( node1 . value ) . toBe ( 1 ) ;
2829 expect ( node2 . value ) . toBe ( 2 ) ;
@@ -33,21 +34,53 @@ describe('LinkedList', () => {
3334 it ( 'should delete node by value from linked list' , ( ) => {
3435 const linkedList = new LinkedList ( ) ;
3536
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 } ) ;
4243
43- const deletedNode = linkedList . delete ( 3 ) ;
44+ const deletedNode = linkedList . deleteByValue ( 3 ) ;
4445 expect ( deletedNode . value ) . toBe ( 3 ) ;
4546 expect ( linkedList . toString ( ) ) . toBe ( '1,2,3,4,5' ) ;
4647
47- linkedList . delete ( 3 ) ;
48+ linkedList . deleteByValue ( 3 ) ;
4849 expect ( linkedList . toString ( ) ) . toBe ( '1,2,4,5' ) ;
4950
50- linkedList . delete ( 1 ) ;
51+ linkedList . deleteByValue ( 1 ) ;
5152 expect ( linkedList . toString ( ) ) . toBe ( '2,4,5' ) ;
5253 } ) ;
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+ } ) ;
5386} ) ;
0 commit comments