-
Notifications
You must be signed in to change notification settings - Fork 20k
Add method to get middle node for SinglyLinkedList and unit test for SinglyLinkedList #3913
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
src/test/java/com/thealgorithms/datastructures/lists/SinglyLinkedListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.thealgorithms.datastructures.lists; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class SinglyLinkedListTest { | ||
|
||
/** | ||
* Initialize a list with natural order values with pre-defined length | ||
* @param length | ||
* @return linked list with pre-defined number of nodes | ||
*/ | ||
private SinglyLinkedList createSampleList(int length) { | ||
List<Node> nodeList = new ArrayList<>(); | ||
for (int i = 1; i <= length; i++) { | ||
Node node = new Node(i); | ||
nodeList.add(node); | ||
} | ||
|
||
for (int i = 0; i < length - 1; i++) { | ||
nodeList.get(i).next = nodeList.get(i+1); | ||
} | ||
|
||
return new SinglyLinkedList(nodeList.get(0), length); | ||
} | ||
|
||
@Test | ||
void detectLoop() { | ||
//List has cycle | ||
Node firstNode = new Node(1); | ||
Node secondNode = new Node(2); | ||
Node thirdNode = new Node(3); | ||
Node fourthNode = new Node(4); | ||
|
||
firstNode.next = secondNode; | ||
secondNode.next = thirdNode; | ||
thirdNode.next = fourthNode; | ||
fourthNode.next = firstNode; | ||
|
||
SinglyLinkedList listHasLoop = new SinglyLinkedList(firstNode, 4); | ||
assertTrue(listHasLoop.detectLoop()); | ||
|
||
SinglyLinkedList listHasNoLoop = createSampleList(5); | ||
assertFalse(listHasNoLoop.detectLoop()); | ||
} | ||
|
||
@Test | ||
void middle() { | ||
int oddNumberOfNode = 7; | ||
SinglyLinkedList list = createSampleList(oddNumberOfNode); | ||
assertEquals(oddNumberOfNode/2 + 1, list.middle().value); | ||
int evenNumberOfNode = 8; | ||
list = createSampleList(evenNumberOfNode); | ||
assertEquals(evenNumberOfNode/2, list.middle().value); | ||
|
||
//return null if empty | ||
list = new SinglyLinkedList(); | ||
assertNull(list.middle()); | ||
|
||
//return head if there is only one node | ||
list = createSampleList(1); | ||
assertEquals(list.getHead(), list.middle()); | ||
} | ||
|
||
@Test | ||
void swap() { | ||
SinglyLinkedList list = createSampleList(5); | ||
assertEquals(1, list.getHead().value); | ||
assertEquals(5, list.getNth(4)); | ||
list.swapNodes(1,5); | ||
assertEquals(5, list.getHead().value); | ||
assertEquals(1, list.getNth(4)); | ||
} | ||
|
||
@Test | ||
void clear() { | ||
SinglyLinkedList list = createSampleList(5); | ||
assertEquals(5, list.size()); | ||
list.clear(); | ||
assertEquals(0, list.size()); | ||
assertTrue(list.isEmpty()); | ||
} | ||
|
||
@Test | ||
void search() { | ||
SinglyLinkedList list = createSampleList(10); | ||
assertTrue(list.search(5)); | ||
assertFalse(list.search(20)); | ||
} | ||
|
||
@Test | ||
void deleteNth() { | ||
SinglyLinkedList list = createSampleList(10); | ||
assertTrue(list.search(7)); | ||
list.deleteNth(6); //Index 6 has value 7 | ||
assertFalse(list.search(7)); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.