44import 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 */
1327public 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 ;
0 commit comments