Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Git line ending warnings #2041

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -19,17 +19,28 @@ describe('reverseTraversal', () => {

expect(traversedNodeValues).toEqual([3, 2, 1]);
});
});

// it('should reverse traversal the linked list with callback', () => {
// const linkedList = new LinkedList();
//
// linkedList
// .append(1)
// .append(2)
// .append(3);
//
// expect(linkedList.toString()).toBe('1,2,3');
// expect(linkedList.reverseTraversal(linkedList.head, value => value * 2)).toEqual([6, 4, 2]);
// expect(() => linkedList.reverseTraversal(linkedList.head)).toThrow();
// });
// New Test: Should throw an error if no callback is provided
it('should throw an error if no callback function is provided', () => {
const linkedList = new LinkedList();
linkedList.append(1);

expect(() => reverseTraversal(linkedList)).toThrow('Callback function is required for reverseTraversal');
});

// New Test: Should throw an error if the linked list is empty
it('should throw an error if linked list is empty', () => {
const linkedList = new LinkedList();

expect(() => reverseTraversal(linkedList, (value) => console.log(value))).toThrow(
'Cannot reverse traverse an empty linked list',
);
});

// New Test: Should throw an error if linked list is null
it('should throw an error if linked list is null', () => {
expect(() => reverseTraversal(null, (value) => console.log(value))).toThrow(
'Cannot reverse traverse an empty linked list',
);
});
});
10 changes: 10 additions & 0 deletions src/algorithms/linked-list/reverse-traversal/reverseTraversal.js
Original file line number Diff line number Diff line change
@@ -20,5 +20,15 @@ function reverseTraversalRecursive(node, callback) {
* @param {traversalCallback} callback
*/
export default function reverseTraversal(linkedList, callback) {
// Check if the callback function is provided and is a function
if (!callback || typeof callback !== 'function') {
throw new Error('Callback function is required for reverseTraversal');
}

// Check if the linked list is empty
if (!linkedList || !linkedList.head) {
throw new Error('Cannot reverse traverse an empty linked list');
}

reverseTraversalRecursive(linkedList.head, callback);
}