Skip to content

Commit cc23698

Browse files
committedFeb 13, 2025
Fixed Git line ending warnings
1 parent e40a67b commit cc23698

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed
 

‎src/algorithms/linked-list/reverse-traversal/__test__/reverseTraversal.test.js

+24-13
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,28 @@ describe('reverseTraversal', () => {
1919

2020
expect(traversedNodeValues).toEqual([3, 2, 1]);
2121
});
22-
});
2322

24-
// it('should reverse traversal the linked list with callback', () => {
25-
// const linkedList = new LinkedList();
26-
//
27-
// linkedList
28-
// .append(1)
29-
// .append(2)
30-
// .append(3);
31-
//
32-
// expect(linkedList.toString()).toBe('1,2,3');
33-
// expect(linkedList.reverseTraversal(linkedList.head, value => value * 2)).toEqual([6, 4, 2]);
34-
// expect(() => linkedList.reverseTraversal(linkedList.head)).toThrow();
35-
// });
23+
// New Test: Should throw an error if no callback is provided
24+
it('should throw an error if no callback function is provided', () => {
25+
const linkedList = new LinkedList();
26+
linkedList.append(1);
27+
28+
expect(() => reverseTraversal(linkedList)).toThrow('Callback function is required for reverseTraversal');
29+
});
30+
31+
// New Test: Should throw an error if the linked list is empty
32+
it('should throw an error if linked list is empty', () => {
33+
const linkedList = new LinkedList();
34+
35+
expect(() => reverseTraversal(linkedList, (value) => console.log(value))).toThrow(
36+
'Cannot reverse traverse an empty linked list',
37+
);
38+
});
39+
40+
// New Test: Should throw an error if linked list is null
41+
it('should throw an error if linked list is null', () => {
42+
expect(() => reverseTraversal(null, (value) => console.log(value))).toThrow(
43+
'Cannot reverse traverse an empty linked list',
44+
);
45+
});
46+
});

‎src/algorithms/linked-list/reverse-traversal/reverseTraversal.js

+10
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,15 @@ function reverseTraversalRecursive(node, callback) {
2020
* @param {traversalCallback} callback
2121
*/
2222
export default function reverseTraversal(linkedList, callback) {
23+
// Check if the callback function is provided and is a function
24+
if (!callback || typeof callback !== 'function') {
25+
throw new Error('Callback function is required for reverseTraversal');
26+
}
27+
28+
// Check if the linked list is empty
29+
if (!linkedList || !linkedList.head) {
30+
throw new Error('Cannot reverse traverse an empty linked list');
31+
}
32+
2333
reverseTraversalRecursive(linkedList.head, callback);
2434
}

0 commit comments

Comments
 (0)
Please sign in to comment.