4
4
import java .util .List ;
5
5
6
6
/**
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:
11
15
* 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>
12
26
*/
13
27
public class SortedLinkedList {
14
28
private Node head ;
15
29
private Node tail ;
16
30
31
+ /**
32
+ * Initializes an empty sorted linked list.
33
+ */
17
34
public SortedLinkedList () {
18
35
this .head = null ;
19
36
this .tail = null ;
20
37
}
21
38
22
39
/**
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
25
43
*/
26
44
public void insert (int value ) {
27
45
Node newNode = new Node (value );
@@ -48,16 +66,10 @@ public void insert(int value) {
48
66
}
49
67
50
68
/**
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
61
73
*/
62
74
public boolean delete (int value ) {
63
75
if (this .head == null ) {
@@ -87,9 +99,10 @@ public boolean delete(int value) {
87
99
}
88
100
89
101
/**
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
93
106
*/
94
107
public boolean search (int value ) {
95
108
Node temp = this .head ;
@@ -103,14 +116,17 @@ public boolean search(int value) {
103
116
}
104
117
105
118
/**
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
108
122
*/
109
123
public boolean isEmpty () {
110
124
return head == null ;
111
125
}
126
+
112
127
/**
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
+ *
114
130
* @return a string representation of the sorted linked list
115
131
*/
116
132
@ Override
@@ -123,12 +139,14 @@ public String toString() {
123
139
temp = temp .next ;
124
140
}
125
141
return "[" + String .join (", " , elements ) + "]" ;
126
-
127
142
} else {
128
143
return "[]" ;
129
144
}
130
145
}
131
146
147
+ /**
148
+ * Node represents an element in the sorted linked list.
149
+ */
132
150
public final class Node {
133
151
public final int value ;
134
152
public Node next ;
0 commit comments