Skip to content

Commit d0962bc

Browse files
Merge pull request TheAlgorithms#1477 from shellhub/dev
Updated SinglyLinkedList
2 parents db86674 + d4e16ab commit d0962bc

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,14 @@
6060
* [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MinPriorityQueue.java)
6161
* Lists
6262
* [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)
6364
* [CursorLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CursorLinkedList.java)
6465
* [DoublyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/DoublyLinkedList.java)
6566
* [ListAddnFun](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/ListAddnFun.java)
6667
* [Merge K SortedLinkedlist](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/Merge_K_SortedLinkedlist.java)
6768
* [MergeSortedArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedArrayList.java)
6869
* [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)
6971
* [SinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java)
7072
* Queues
7173
* [GenericArrayListQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

DataStructures/Lists/SinglyLinkedList.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,39 @@ public int count() {
194194
return count;
195195
}
196196

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+
197230
@Override
198231
public String toString() {
199232
if (size == 0) {
@@ -227,6 +260,17 @@ public static void main(String[] arg) {
227260
list.insertNth(1, 4);
228261
assert list.toString().equals("10->7->5->3->1");
229262

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+
230274
/* Test delete function */
231275
list.deleteHead();
232276
list.deleteNth(1);

0 commit comments

Comments
 (0)