mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-05 08:17:33 +08:00
Enhance docs, add tests in SortedLinkedList
(#6014)
This commit is contained in:
@ -4,24 +4,42 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
|
* The SortedLinkedList class represents a singly linked list that maintains its elements in sorted order.
|
||||||
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
|
* Elements are ordered based on their natural ordering, with smaller elements at the head and larger elements toward the tail.
|
||||||
* This implementation uses a singly linked list to store the elements.
|
* The class provides methods for inserting, deleting, and searching elements, as well as checking if the list is empty.
|
||||||
* Further details can be found on this link
|
* <p>
|
||||||
|
* This implementation utilizes a singly linked list to maintain a dynamically sorted list.
|
||||||
|
* </p>
|
||||||
|
* <p>
|
||||||
|
* Further information can be found here:
|
||||||
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
|
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <b>Usage Example:</b>
|
||||||
|
* <pre>
|
||||||
|
* SortedLinkedList list = new SortedLinkedList();
|
||||||
|
* list.insert(10);
|
||||||
|
* list.insert(5);
|
||||||
|
* list.insert(20);
|
||||||
|
* System.out.println(list); // Outputs: [5, 10, 20]
|
||||||
|
* </pre>
|
||||||
*/
|
*/
|
||||||
public class SortedLinkedList {
|
public class SortedLinkedList {
|
||||||
private Node head;
|
private Node head;
|
||||||
private Node tail;
|
private Node tail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes an empty sorted linked list.
|
||||||
|
*/
|
||||||
public SortedLinkedList() {
|
public SortedLinkedList() {
|
||||||
this.head = null;
|
this.head = null;
|
||||||
this.tail = null;
|
this.tail = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Inserts a new element into the sorted linked list.
|
* Inserts a new integer into the list, maintaining sorted order.
|
||||||
* @param value the value to be inserted
|
*
|
||||||
|
* @param value the integer to insert
|
||||||
*/
|
*/
|
||||||
public void insert(int value) {
|
public void insert(int value) {
|
||||||
Node newNode = new Node(value);
|
Node newNode = new Node(value);
|
||||||
@ -48,16 +66,10 @@ public class SortedLinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Displays the elements of the sorted linked list.
|
* Deletes the first occurrence of a specified integer in the list.
|
||||||
*/
|
*
|
||||||
public void display() {
|
* @param value the integer to delete
|
||||||
System.out.println(this.toString());
|
* @return {@code true} if the element was found and deleted; {@code false} otherwise
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Deletes the first occurrence of the specified element in the sorted linked list.
|
|
||||||
* @param value the value to be deleted
|
|
||||||
* @return true if the element is found and deleted, false otherwise
|
|
||||||
*/
|
*/
|
||||||
public boolean delete(int value) {
|
public boolean delete(int value) {
|
||||||
if (this.head == null) {
|
if (this.head == null) {
|
||||||
@ -87,9 +99,10 @@ public class SortedLinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches for the specified element in the sorted linked list.
|
* Searches for a specified integer in the list.
|
||||||
* @param value the value to be searched
|
*
|
||||||
* @return true if the element is found, false otherwise
|
* @param value the integer to search for
|
||||||
|
* @return {@code true} if the value is present in the list; {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
public boolean search(int value) {
|
public boolean search(int value) {
|
||||||
Node temp = this.head;
|
Node temp = this.head;
|
||||||
@ -103,14 +116,17 @@ public class SortedLinkedList {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Checks if the sorted linked list is empty.
|
* Checks if the list is empty.
|
||||||
* @return true if the list is empty, false otherwise
|
*
|
||||||
|
* @return {@code true} if the list is empty; {@code false} otherwise
|
||||||
*/
|
*/
|
||||||
public boolean isEmpty() {
|
public boolean isEmpty() {
|
||||||
return head == null;
|
return head == null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a string representation of the sorted linked list.
|
* Returns a string representation of the sorted linked list in the format [element1, element2, ...].
|
||||||
|
*
|
||||||
* @return a string representation of the sorted linked list
|
* @return a string representation of the sorted linked list
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
@ -123,12 +139,14 @@ public class SortedLinkedList {
|
|||||||
temp = temp.next;
|
temp = temp.next;
|
||||||
}
|
}
|
||||||
return "[" + String.join(", ", elements) + "]";
|
return "[" + String.join(", ", elements) + "]";
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return "[]";
|
return "[]";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Node represents an element in the sorted linked list.
|
||||||
|
*/
|
||||||
public final class Node {
|
public final class Node {
|
||||||
public final int value;
|
public final int value;
|
||||||
public Node next;
|
public Node next;
|
||||||
|
@ -4,13 +4,26 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
public class SortedLinkedListTest {
|
public class SortedLinkedListTest {
|
||||||
|
|
||||||
|
private SortedLinkedList list;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
list = new SortedLinkedList();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInsert() {
|
public void testInsertIntoEmptyList() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
list.insert(5);
|
||||||
|
assertEquals("[5]", list.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInsertInSortedOrder() {
|
||||||
list.insert(5);
|
list.insert(5);
|
||||||
list.insert(3);
|
list.insert(3);
|
||||||
list.insert(7);
|
list.insert(7);
|
||||||
@ -18,48 +31,99 @@ public class SortedLinkedListTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDelete() {
|
public void testInsertDuplicateValues() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
|
||||||
list.insert(5);
|
list.insert(5);
|
||||||
list.insert(3);
|
list.insert(5);
|
||||||
list.insert(7);
|
list.insert(5);
|
||||||
assertTrue(list.delete(5));
|
assertEquals("[5, 5, 5]", list.toString());
|
||||||
assertEquals("[3, 7]", list.toString());
|
|
||||||
assertFalse(list.delete(10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSearch() {
|
public void testDeleteHeadElement() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
list.insert(1);
|
||||||
list.insert(5);
|
list.insert(2);
|
||||||
list.insert(3);
|
list.insert(3);
|
||||||
list.insert(7);
|
assertTrue(list.delete(1));
|
||||||
assertTrue(list.search(5));
|
assertEquals("[2, 3]", list.toString());
|
||||||
assertFalse(list.search(10));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyList() {
|
public void testDeleteTailElement() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
list.insert(1);
|
||||||
|
list.insert(2);
|
||||||
|
list.insert(3);
|
||||||
|
assertTrue(list.delete(3));
|
||||||
|
assertEquals("[1, 2]", list.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteMiddleElement() {
|
||||||
|
list.insert(1);
|
||||||
|
list.insert(2);
|
||||||
|
list.insert(3);
|
||||||
|
assertTrue(list.delete(2));
|
||||||
|
assertEquals("[1, 3]", list.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteNonexistentElement() {
|
||||||
|
list.insert(1);
|
||||||
|
list.insert(2);
|
||||||
|
assertFalse(list.delete(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteFromSingleElementList() {
|
||||||
|
list.insert(5);
|
||||||
|
assertTrue(list.delete(5));
|
||||||
assertEquals("[]", list.toString());
|
assertEquals("[]", list.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDeleteFromEmptyList() {
|
||||||
assertFalse(list.delete(5));
|
assertFalse(list.delete(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSearchInEmptyList() {
|
||||||
assertFalse(list.search(5));
|
assertFalse(list.search(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSearchForExistingElement() {
|
||||||
|
list.insert(3);
|
||||||
|
list.insert(1);
|
||||||
|
list.insert(5);
|
||||||
|
assertTrue(list.search(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSearchForNonexistentElement() {
|
||||||
|
list.insert(3);
|
||||||
|
list.insert(1);
|
||||||
|
list.insert(5);
|
||||||
|
assertFalse(list.search(10));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsEmptyOnEmptyList() {
|
public void testIsEmptyOnEmptyList() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
|
||||||
assertTrue(list.isEmpty());
|
assertTrue(list.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsEmptyOnNonEmptyList() {
|
public void testIsEmptyOnNonEmptyList() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
list.insert(10);
|
||||||
|
assertFalse(list.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsEmptyAfterInsertion() {
|
||||||
list.insert(10);
|
list.insert(10);
|
||||||
assertFalse(list.isEmpty());
|
assertFalse(list.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testIsEmptyAfterDeletion() {
|
public void testIsEmptyAfterDeletion() {
|
||||||
SortedLinkedList list = new SortedLinkedList();
|
|
||||||
list.insert(10);
|
list.insert(10);
|
||||||
list.delete(10);
|
list.delete(10);
|
||||||
assertTrue(list.isEmpty());
|
assertTrue(list.isEmpty());
|
||||||
|
Reference in New Issue
Block a user