diff --git a/src/main/java/com/thealgorithms/searches/JumpSearch.java b/src/main/java/com/thealgorithms/searches/JumpSearch.java index cbc494c8c16a..e93738f1cb28 100644 --- a/src/main/java/com/thealgorithms/searches/JumpSearch.java +++ b/src/main/java/com/thealgorithms/searches/JumpSearch.java @@ -27,6 +27,17 @@ * Result: Index = 4 * *
+ * Example (Simple):
+ * Input: arr = [2, 4, 6, 8, 10], key = 8
+ * Output: 3
+ *
+ *
+ * Explanation (Easy):
+ * Instead of checking every element one by one, Jump Search skips elements
+ * by jumping ahead fixed steps (√n). Once it finds a range where the element
+ * might exist, it performs a linear search in that smaller block.
+ *
+ *
* Time Complexity:
* - Best-case: O(1) - element found at first position
* - Average: O(√n) - optimal block size reduces jumps
@@ -36,6 +47,13 @@
* Space Complexity: O(1) - only uses a constant amount of extra space
*
*
+ * Edge Cases: + *
* Note: Jump Search requires a sorted array. For unsorted arrays, use Linear Search. * Compared to Linear Search (O(n)), Jump Search is faster for large arrays. * Compared to Binary Search (O(log n)), Jump Search is less efficient but may be diff --git a/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java new file mode 100644 index 000000000000..daec1748735f --- /dev/null +++ b/src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java @@ -0,0 +1,70 @@ +package com.thealgorithms.stacks; + +/** + * A class that implements a Stack using a singly linked list. + * Supports basic operations like push, pop, peek, and isEmpty. + * + * Reference: https://www.geeksforgeeks.org/stack-using-linked-list/ + */ +public class StackUsingLinkedList { + + /** + * Node class representing each element in the stack + */ + private static class Node { + int data; + Node next; + + Node(int data) { + this.data = data; + } + } + + private Node top; + + /** + * Push an element onto the stack + * + * @param value the value to push + */ + public void push(int value) { + Node newNode = new Node(value); + newNode.next = top; + top = newNode; + } + + /** + * Remove and return the top element of the stack + * + * @return top element + */ + public int pop() { + if (top == null) { + throw new RuntimeException("Stack is empty"); + } + int value = top.data; + top = top.next; + return value; + } + + /** + * Return the top element without removing + * + * @return top element + */ + public int peek() { + if (top == null) { + throw new RuntimeException("Stack is empty"); + } + return top.data; + } + + /** + * Check if the stack is empty + * + * @return true if empty, false otherwise + */ + public boolean isEmpty() { + return top == null; + } +} diff --git a/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java new file mode 100644 index 000000000000..c06604fa95fd --- /dev/null +++ b/src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java @@ -0,0 +1,75 @@ +package com.thealgorithms.stacks; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +/** + * Test class for StackUsingLinkedList. + * + * This class contains unit tests to verify the correctness + * of stack operations such as push, pop, peek, and isEmpty. + * + * Reference: https://www.geeksforgeeks.org/stack-using-linked-list/ + */ +class StackUsingLinkedListTest { + + /** + * Test push and pop operations + */ + @Test + void testPushAndPop() { + StackUsingLinkedList stack = new StackUsingLinkedList(); + stack.push(10); + stack.push(20); + + assertEquals(20, stack.pop()); + assertEquals(10, stack.pop()); + } + + /** + * Test peek operation + */ + @Test + void testPeek() { + StackUsingLinkedList stack = new StackUsingLinkedList(); + stack.push(5); + + assertEquals(5, stack.peek()); + } + + /** + * Test isEmpty method + */ + @Test + void testIsEmpty() { + StackUsingLinkedList stack = new StackUsingLinkedList(); + + assertTrue(stack.isEmpty()); + stack.push(1); + assertFalse(stack.isEmpty()); + } + + /** + * Test pop on empty stack (edge case) + */ + @Test + void testPopOnEmptyStack() { + StackUsingLinkedList stack = new StackUsingLinkedList(); + + assertThrows(RuntimeException.class, stack::pop); + } + + /** + * Test peek on empty stack (edge case) + */ + @Test + void testPeekOnEmptyStack() { + StackUsingLinkedList stack = new StackUsingLinkedList(); + + assertThrows(RuntimeException.class, stack::peek); + } +}