mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-27 06:23:08 +08:00
Sorted Linked List Added (#5519)
* Sorted Linked List added with Javadoc and tests * "Added comments to SortedLinkedList.java to describe the implementation and provide a reference link." * Upgraded test from junit 4 to junit 5 * Added space before braces of functions * Rename SortedlinkedListTest.java to SortedLinkedListTest.java * made to string null safe * Updated tail * "Added assignment of `this.tail` to `newNode` in `SortedLinkedList` class." * Remove assertions for minValue and maxValue in empty list test * tried to get link updated * "Fixed whitespace and formatting issues in SortedLinkedList.java and SortedLinkedListTest.java" * formatting of test file corrected * Removed few whitespaces * Addressed comments by alxkm * "Updated toString method to include brackets and removed default Node constructor." * tests updated
This commit is contained in:

committed by
GitHub

parent
013d122e7d
commit
d436910ac4
@ -0,0 +1,67 @@
|
||||
package com.thealgorithms.datastructures.lists;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class SortedLinkedListTest {
|
||||
|
||||
@Test
|
||||
public void testInsert() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(5);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
assertEquals("[3, 5, 7]", list.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(5);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
assertTrue(list.delete(5));
|
||||
assertEquals("[3, 7]", list.toString());
|
||||
assertFalse(list.delete(10));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSearch() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(5);
|
||||
list.insert(3);
|
||||
list.insert(7);
|
||||
assertTrue(list.search(5));
|
||||
assertFalse(list.search(10));
|
||||
}
|
||||
@Test
|
||||
public void testEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
assertEquals("[]", list.toString());
|
||||
assertFalse(list.delete(5));
|
||||
assertFalse(list.search(5));
|
||||
}
|
||||
@Test
|
||||
public void testIsEmptyOnEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyOnNonEmptyList() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(10);
|
||||
assertFalse(list.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsEmptyAfterDeletion() {
|
||||
SortedLinkedList list = new SortedLinkedList();
|
||||
list.insert(10);
|
||||
list.delete(10);
|
||||
assertTrue(list.isEmpty());
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user