File tree 4 files changed +104
-0
lines changed
4 files changed +104
-0
lines changed Original file line number Diff line number Diff line change 60
60
* [ MinPriorityQueue] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MinPriorityQueue.java )
61
61
* Lists
62
62
* [ CircleLinkedList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CircleLinkedList.java )
63
+ * [ CountSinglyLinkedListRecursion] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CountSinglyLinkedListRecursion.java )
63
64
* [ CursorLinkedList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CursorLinkedList.java )
64
65
* [ DoublyLinkedList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/DoublyLinkedList.java )
65
66
* [ ListAddnFun] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/ListAddnFun.java )
66
67
* [ Merge K SortedLinkedlist] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/Merge_K_SortedLinkedlist.java )
67
68
* [ MergeSortedArrayList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedArrayList.java )
68
69
* [ MergeSortedSinglyLinkedList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedSinglyLinkedList.java )
70
+ * [ SearchSinglyLinkedListRecursion] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SearchSinglyLinkedListRecursion.java )
69
71
* [ SinglyLinkedList] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java )
70
72
* Queues
71
73
* [ GenericArrayListQueue] ( https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java )
Original file line number Diff line number Diff line change
1
+ package DataStructures .Lists ;
2
+
3
+ public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
4
+ public static void main (String [] args ) {
5
+ CountSinglyLinkedListRecursion list = new CountSinglyLinkedListRecursion ();
6
+ for (int i = 1 ; i <= 5 ; ++i ) {
7
+ list .insert (i );
8
+ }
9
+ assert list .count () == 5 ;
10
+ }
11
+
12
+ /**
13
+ * Calculate the count of the list manually using recursion.
14
+ *
15
+ * @param head head of the list.
16
+ * @return count of the list.
17
+ */
18
+ private int countRecursion (Node head ) {
19
+ return head == null ? 0 : 1 + countRecursion (head .next );
20
+ }
21
+
22
+ @ Override
23
+ public int count () {
24
+ return countRecursion (getHead ());
25
+ }
26
+ }
Original file line number Diff line number Diff line change
1
+ package DataStructures .Lists ;
2
+
3
+ public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
4
+ public static void main (String [] args ) {
5
+ SearchSinglyLinkedListRecursion list = new SearchSinglyLinkedListRecursion ();
6
+ for (int i = 1 ; i <= 10 ; ++i ) {
7
+ list .insert (i );
8
+ }
9
+
10
+ for (int i = 1 ; i <= 10 ; ++i ) {
11
+ assert list .search (i );
12
+ }
13
+ assert !list .search (-1 )
14
+ && !list .search (100 );
15
+ }
16
+
17
+ /**
18
+ * Test if the value key is present in the list using recursion.
19
+ *
20
+ * @param node the head node.
21
+ * @param key the value to be searched.
22
+ * @return {@code true} if key is present in the list, otherwise {@code false}.
23
+ */
24
+ private boolean searchRecursion (Node node , int key ) {
25
+ return node != null && (node .value == key || searchRecursion (node .next , key ));
26
+ }
27
+
28
+ @ Override
29
+ public boolean search (int key ) {
30
+ return searchRecursion (getHead (), key );
31
+ }
32
+ }
Original file line number Diff line number Diff line change @@ -194,6 +194,39 @@ public int count() {
194
194
return count ;
195
195
}
196
196
197
+ /**
198
+ * Test if the value key is present in the list.
199
+ *
200
+ * @param key the value to be searched.
201
+ * @return {@code true} if key is present in the list, otherwise {@code false}.
202
+ */
203
+ public boolean search (int key ) {
204
+ Node cur = head ;
205
+ while (cur != null ) {
206
+ if (cur .value == key ) {
207
+ return true ;
208
+ }
209
+ cur = cur .next ;
210
+ }
211
+ return false ;
212
+ }
213
+
214
+ /**
215
+ * Return element at special index.
216
+ *
217
+ * @param index given index of element
218
+ * @return element at special index.
219
+ */
220
+ public int getNth (int index ) {
221
+ checkBounds (index , 0 , size - 1 );
222
+ Node cur = head ;
223
+ for (int i = 0 ; i < index ; ++i ) {
224
+ cur = cur .next ;
225
+ }
226
+ return cur .value ;
227
+ }
228
+
229
+
197
230
@ Override
198
231
public String toString () {
199
232
if (size == 0 ) {
@@ -227,6 +260,17 @@ public static void main(String[] arg) {
227
260
list .insertNth (1 , 4 );
228
261
assert list .toString ().equals ("10->7->5->3->1" );
229
262
263
+ /* Test search function */
264
+ assert list .search (10 )
265
+ && list .search (5 )
266
+ && list .search (1 )
267
+ && !list .search (100 );
268
+
269
+ /* Test get function */
270
+ assert list .getNth (0 ) == 10
271
+ && list .getNth (2 ) == 5
272
+ && list .getNth (4 ) == 1 ;
273
+
230
274
/* Test delete function */
231
275
list .deleteHead ();
232
276
list .deleteNth (1 );
You can’t perform that action at this time.
0 commit comments