Skip to content

Commit 59a8e1d

Browse files
authored
Enhance docs, add tests in SortedLinkedList (#6014)
1 parent 2083d68 commit 59a8e1d

File tree

2 files changed

+124
-42
lines changed

2 files changed

+124
-42
lines changed

src/main/java/com/thealgorithms/datastructures/lists/SortedLinkedList.java

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,42 @@
44
import java.util.List;
55

66
/**
7-
* A SortedLinkedList is a data structure that maintains a sorted list of elements.
8-
* Elements are ordered based on their natural ordering or by a Comparator provided at the time of creation.
9-
* This implementation uses a singly linked list to store the elements.
10-
* Further details can be found on this link
7+
* The SortedLinkedList class represents a singly linked list that maintains its elements in sorted order.
8+
* Elements are ordered based on their natural ordering, with smaller elements at the head and larger elements toward the tail.
9+
* The class provides methods for inserting, deleting, and searching elements, as well as checking if the list is empty.
10+
* <p>
11+
* This implementation utilizes a singly linked list to maintain a dynamically sorted list.
12+
* </p>
13+
* <p>
14+
* Further information can be found here:
1115
* https://runestone.academy/ns/books/published/cppds/LinearLinked/ImplementinganOrderedList.html
16+
* </p>
17+
*
18+
* <b>Usage Example:</b>
19+
* <pre>
20+
* SortedLinkedList list = new SortedLinkedList();
21+
* list.insert(10);
22+
* list.insert(5);
23+
* list.insert(20);
24+
* System.out.println(list); // Outputs: [5, 10, 20]
25+
* </pre>
1226
*/
1327
public class SortedLinkedList {
1428
private Node head;
1529
private Node tail;
1630

31+
/**
32+
* Initializes an empty sorted linked list.
33+
*/
1734
public SortedLinkedList() {
1835
this.head = null;
1936
this.tail = null;
2037
}
2138

2239
/**
23-
* Inserts a new element into the sorted linked list.
24-
* @param value the value to be inserted
40+
* Inserts a new integer into the list, maintaining sorted order.
41+
*
42+
* @param value the integer to insert
2543
*/
2644
public void insert(int value) {
2745
Node newNode = new Node(value);
@@ -48,16 +66,10 @@ public void insert(int value) {
4866
}
4967

5068
/**
51-
* Displays the elements of the sorted linked list.
52-
*/
53-
public void display() {
54-
System.out.println(this.toString());
55-
}
56-
57-
/**
58-
* Deletes the first occurrence of the specified element in the sorted linked list.
59-
* @param value the value to be deleted
60-
* @return true if the element is found and deleted, false otherwise
69+
* Deletes the first occurrence of a specified integer in the list.
70+
*
71+
* @param value the integer to delete
72+
* @return {@code true} if the element was found and deleted; {@code false} otherwise
6173
*/
6274
public boolean delete(int value) {
6375
if (this.head == null) {
@@ -87,9 +99,10 @@ public boolean delete(int value) {
8799
}
88100

89101
/**
90-
* Searches for the specified element in the sorted linked list.
91-
* @param value the value to be searched
92-
* @return true if the element is found, false otherwise
102+
* Searches for a specified integer in the list.
103+
*
104+
* @param value the integer to search for
105+
* @return {@code true} if the value is present in the list; {@code false} otherwise
93106
*/
94107
public boolean search(int value) {
95108
Node temp = this.head;
@@ -103,14 +116,17 @@ public boolean search(int value) {
103116
}
104117

105118
/**
106-
* Checks if the sorted linked list is empty.
107-
* @return true if the list is empty, false otherwise
119+
* Checks if the list is empty.
120+
*
121+
* @return {@code true} if the list is empty; {@code false} otherwise
108122
*/
109123
public boolean isEmpty() {
110124
return head == null;
111125
}
126+
112127
/**
113-
* Returns a string representation of the sorted linked list.
128+
* Returns a string representation of the sorted linked list in the format [element1, element2, ...].
129+
*
114130
* @return a string representation of the sorted linked list
115131
*/
116132
@Override
@@ -123,12 +139,14 @@ public String toString() {
123139
temp = temp.next;
124140
}
125141
return "[" + String.join(", ", elements) + "]";
126-
127142
} else {
128143
return "[]";
129144
}
130145
}
131146

147+
/**
148+
* Node represents an element in the sorted linked list.
149+
*/
132150
public final class Node {
133151
public final int value;
134152
public Node next;

src/test/java/com/thealgorithms/datastructures/lists/SortedLinkedListTest.java

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,126 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55
import static org.junit.jupiter.api.Assertions.assertTrue;
66

7+
import org.junit.jupiter.api.BeforeEach;
78
import org.junit.jupiter.api.Test;
89

910
public class SortedLinkedListTest {
1011

12+
private SortedLinkedList list;
13+
14+
@BeforeEach
15+
public void setUp() {
16+
list = new SortedLinkedList();
17+
}
18+
19+
@Test
20+
public void testInsertIntoEmptyList() {
21+
list.insert(5);
22+
assertEquals("[5]", list.toString());
23+
}
24+
1125
@Test
12-
public void testInsert() {
13-
SortedLinkedList list = new SortedLinkedList();
26+
public void testInsertInSortedOrder() {
1427
list.insert(5);
1528
list.insert(3);
1629
list.insert(7);
1730
assertEquals("[3, 5, 7]", list.toString());
1831
}
1932

2033
@Test
21-
public void testDelete() {
22-
SortedLinkedList list = new SortedLinkedList();
34+
public void testInsertDuplicateValues() {
2335
list.insert(5);
36+
list.insert(5);
37+
list.insert(5);
38+
assertEquals("[5, 5, 5]", list.toString());
39+
}
40+
41+
@Test
42+
public void testDeleteHeadElement() {
43+
list.insert(1);
44+
list.insert(2);
2445
list.insert(3);
25-
list.insert(7);
26-
assertTrue(list.delete(5));
27-
assertEquals("[3, 7]", list.toString());
28-
assertFalse(list.delete(10));
46+
assertTrue(list.delete(1));
47+
assertEquals("[2, 3]", list.toString());
2948
}
3049

3150
@Test
32-
public void testSearch() {
33-
SortedLinkedList list = new SortedLinkedList();
34-
list.insert(5);
51+
public void testDeleteTailElement() {
52+
list.insert(1);
53+
list.insert(2);
3554
list.insert(3);
36-
list.insert(7);
37-
assertTrue(list.search(5));
38-
assertFalse(list.search(10));
55+
assertTrue(list.delete(3));
56+
assertEquals("[1, 2]", list.toString());
57+
}
58+
59+
@Test
60+
public void testDeleteMiddleElement() {
61+
list.insert(1);
62+
list.insert(2);
63+
list.insert(3);
64+
assertTrue(list.delete(2));
65+
assertEquals("[1, 3]", list.toString());
66+
}
67+
68+
@Test
69+
public void testDeleteNonexistentElement() {
70+
list.insert(1);
71+
list.insert(2);
72+
assertFalse(list.delete(3));
3973
}
74+
4075
@Test
41-
public void testEmptyList() {
42-
SortedLinkedList list = new SortedLinkedList();
76+
public void testDeleteFromSingleElementList() {
77+
list.insert(5);
78+
assertTrue(list.delete(5));
4379
assertEquals("[]", list.toString());
80+
}
81+
82+
@Test
83+
public void testDeleteFromEmptyList() {
4484
assertFalse(list.delete(5));
85+
}
86+
87+
@Test
88+
public void testSearchInEmptyList() {
4589
assertFalse(list.search(5));
4690
}
91+
92+
@Test
93+
public void testSearchForExistingElement() {
94+
list.insert(3);
95+
list.insert(1);
96+
list.insert(5);
97+
assertTrue(list.search(3));
98+
}
99+
100+
@Test
101+
public void testSearchForNonexistentElement() {
102+
list.insert(3);
103+
list.insert(1);
104+
list.insert(5);
105+
assertFalse(list.search(10));
106+
}
107+
47108
@Test
48109
public void testIsEmptyOnEmptyList() {
49-
SortedLinkedList list = new SortedLinkedList();
50110
assertTrue(list.isEmpty());
51111
}
52112

53113
@Test
54114
public void testIsEmptyOnNonEmptyList() {
55-
SortedLinkedList list = new SortedLinkedList();
115+
list.insert(10);
116+
assertFalse(list.isEmpty());
117+
}
118+
119+
@Test
120+
public void testIsEmptyAfterInsertion() {
56121
list.insert(10);
57122
assertFalse(list.isEmpty());
58123
}
59124

60125
@Test
61126
public void testIsEmptyAfterDeletion() {
62-
SortedLinkedList list = new SortedLinkedList();
63127
list.insert(10);
64128
list.delete(10);
65129
assertTrue(list.isEmpty());

0 commit comments

Comments
 (0)