Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2759173

Browse files
Add unit test to loop-in-list
- Rename loop-in-list function name from `detech` to `detect`
1 parent f6d18ed commit 2759173

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

‎src/_DataStructures_/LinkedList/loop-in-list/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Floyd’s Cycle-Finding Algorithm
22

3-
function detechLoop(linkedList) {
3+
function detectLoop(linkedList) {
44
let slow = linkedList.getFirst();
55
let fast = linkedList.getFirst();
66

@@ -14,3 +14,7 @@ function detechLoop(linkedList) {
1414
}
1515
return false;
1616
}
17+
18+
module.exports = {
19+
detectLoop,
20+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const { LinkedList } = require('../index');
2+
const { detectLoop } = require('.');
3+
4+
describe('Loop a LinkedList', () => {
5+
let loopList = null;
6+
let last = null;
7+
beforeEach(() => {
8+
loopList = new LinkedList();
9+
loopList.addAtBeginning('1');
10+
loopList.addAtEnd('2');
11+
loopList.addAtEnd('3');
12+
loopList.addAtEnd('4');
13+
loopList.addAtEnd('5');
14+
// Create loop in list
15+
last = loopList.getLast();
16+
last.next = loopList.getFirst();
17+
});
18+
19+
it('Should break for empty list', () => {
20+
loopList.delete();
21+
expect(() => detectLoop(loopList)).toThrow(TypeError);
22+
});
23+
24+
it('Should return `true` when looping list', () => {
25+
expect(detectLoop(loopList)).toEqual(true);
26+
});
27+
28+
it('Should return `false` for non loop list', () => {
29+
last.next = null; // remove loop in list
30+
expect(detectLoop(loopList)).toEqual(false);
31+
});
32+
33+
it('Should return `false` for non loop list', () => {
34+
const list = new LinkedList();
35+
list.addAtBeginning('1');
36+
list.addAtEnd('1');
37+
list.addAtEnd('1');
38+
expect(detectLoop(list)).toEqual(false);
39+
});
40+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /