mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-12 14:42:03 +08:00
@ -60,12 +60,14 @@
|
||||
* [MinPriorityQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Heaps/MinPriorityQueue.java)
|
||||
* Lists
|
||||
* [CircleLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CircleLinkedList.java)
|
||||
* [CountSinglyLinkedListRecursion](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CountSinglyLinkedListRecursion.java)
|
||||
* [CursorLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/CursorLinkedList.java)
|
||||
* [DoublyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/DoublyLinkedList.java)
|
||||
* [ListAddnFun](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/ListAddnFun.java)
|
||||
* [Merge K SortedLinkedlist](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/Merge_K_SortedLinkedlist.java)
|
||||
* [MergeSortedArrayList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedArrayList.java)
|
||||
* [MergeSortedSinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/MergeSortedSinglyLinkedList.java)
|
||||
* [SearchSinglyLinkedListRecursion](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SearchSinglyLinkedListRecursion.java)
|
||||
* [SinglyLinkedList](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Lists/SinglyLinkedList.java)
|
||||
* Queues
|
||||
* [GenericArrayListQueue](https://github.com/TheAlgorithms/Java/blob/master/DataStructures/Queues/GenericArrayListQueue.java)
|
||||
|
26
DataStructures/Lists/CountSinglyLinkedListRecursion.java
Normal file
26
DataStructures/Lists/CountSinglyLinkedListRecursion.java
Normal file
@ -0,0 +1,26 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
public class CountSinglyLinkedListRecursion extends SinglyLinkedList {
|
||||
public static void main(String[] args) {
|
||||
CountSinglyLinkedListRecursion list = new CountSinglyLinkedListRecursion();
|
||||
for (int i = 1; i <= 5; ++i) {
|
||||
list.insert(i);
|
||||
}
|
||||
assert list.count() == 5;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the count of the list manually using recursion.
|
||||
*
|
||||
* @param head head of the list.
|
||||
* @return count of the list.
|
||||
*/
|
||||
private int countRecursion(Node head) {
|
||||
return head == null ? 0 : 1 + countRecursion(head.next);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int count() {
|
||||
return countRecursion(getHead());
|
||||
}
|
||||
}
|
32
DataStructures/Lists/SearchSinglyLinkedListRecursion.java
Normal file
32
DataStructures/Lists/SearchSinglyLinkedListRecursion.java
Normal file
@ -0,0 +1,32 @@
|
||||
package DataStructures.Lists;
|
||||
|
||||
public class SearchSinglyLinkedListRecursion extends SinglyLinkedList {
|
||||
public static void main(String[] args) {
|
||||
SearchSinglyLinkedListRecursion list = new SearchSinglyLinkedListRecursion();
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
list.insert(i);
|
||||
}
|
||||
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
assert list.search(i);
|
||||
}
|
||||
assert !list.search(-1)
|
||||
&& !list.search(100);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the value key is present in the list using recursion.
|
||||
*
|
||||
* @param node the head node.
|
||||
* @param key the value to be searched.
|
||||
* @return {@code true} if key is present in the list, otherwise {@code false}.
|
||||
*/
|
||||
private boolean searchRecursion(Node node, int key) {
|
||||
return node != null && (node.value == key || searchRecursion(node.next, key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean search(int key) {
|
||||
return searchRecursion(getHead(), key);
|
||||
}
|
||||
}
|
@ -194,6 +194,39 @@ public class SinglyLinkedList {
|
||||
return count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the value key is present in the list.
|
||||
*
|
||||
* @param key the value to be searched.
|
||||
* @return {@code true} if key is present in the list, otherwise {@code false}.
|
||||
*/
|
||||
public boolean search(int key) {
|
||||
Node cur = head;
|
||||
while (cur != null) {
|
||||
if (cur.value == key) {
|
||||
return true;
|
||||
}
|
||||
cur = cur.next;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return element at special index.
|
||||
*
|
||||
* @param index given index of element
|
||||
* @return element at special index.
|
||||
*/
|
||||
public int getNth(int index) {
|
||||
checkBounds(index, 0, size - 1);
|
||||
Node cur = head;
|
||||
for (int i = 0; i < index; ++i) {
|
||||
cur = cur.next;
|
||||
}
|
||||
return cur.value;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
if (size == 0) {
|
||||
@ -227,6 +260,17 @@ public class SinglyLinkedList {
|
||||
list.insertNth(1, 4);
|
||||
assert list.toString().equals("10->7->5->3->1");
|
||||
|
||||
/* Test search function */
|
||||
assert list.search(10)
|
||||
&& list.search(5)
|
||||
&& list.search(1)
|
||||
&& !list.search(100);
|
||||
|
||||
/* Test get function */
|
||||
assert list.getNth(0) == 10
|
||||
&& list.getNth(2) == 5
|
||||
&& list.getNth(4) == 1;
|
||||
|
||||
/* Test delete function */
|
||||
list.deleteHead();
|
||||
list.deleteNth(1);
|
||||
|
Reference in New Issue
Block a user