Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
ca21a27
docs: add edge cases to JumpSearch documentation
prashantpiyush1111 Apr 14, 2026
4c7ac35
fix: remove trailing whitespace (checkstyle)
prashantpiyush1111 Apr 14, 2026
56a087d
docs: add simple example and beginner-friendly explanation to JumpSearch
prashantpiyush1111 Apr 14, 2026
41020e5
feat: add StackUsingLinkedList implementation with test cases
prashantpiyush1111 Apr 15, 2026
db4e0c3
style: apply clang-format to fix formatting issues
prashantpiyush1111 Apr 15, 2026
43553e1
style: fix checkstyle issues (newline and imports)
prashantpiyush1111 Apr 15, 2026
bedd1ce
style: fix newline at end of StackUsingLinkedList
prashantpiyush1111 Apr 15, 2026
ca2cdfc
style: fix formatting to satisfy clang-format and checkstyle
prashantpiyush1111 Apr 15, 2026
9cd54ee
Merge branch 'master' into master
prashantpiyush1111 Apr 15, 2026
51fff7f
feat: add simple thread-safe queue using synchronized
prashantpiyush1111 Apr 19, 2026
b2199c9
style: fix formatting issues (clang-format)
prashantpiyush1111 Apr 19, 2026
f4b7991
test: add proper unit tests for ThreadSafeQueue
prashantpiyush1111 Apr 19, 2026
4e9244c
fix: resolve checkstyle issues (newline, imports, inner assignment)
prashantpiyush1111 Apr 19, 2026
0f1bc22
style: fix indentation (tabs removed)
prashantpiyush1111 Apr 19, 2026
0ba8aba
style: fix indentation fully (no tabs)
prashantpiyush1111 Apr 19, 2026
d4415ff
style: fix test file indentation (final)
prashantpiyush1111 Apr 19, 2026
3293397
style: final fix test formatting and imports
prashantpiyush1111 Apr 19, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.thealgorithms.datastructures.queues;

public class ThreadSafeQueue<T> {

private static class Node<T> {
T data;
Node<T> next;

Node(T data) {
this.data = data;
}
}

private Node<T> head;
private Node<T> tail;

public synchronized void enqueue(T data) {
Node<T> newNode = new Node<>(data);

if (tail == null) {
head = newNode;
tail = newNode;
return;
}

tail.next = newNode;
tail = newNode;
}

public synchronized T dequeue() {
if (head == null) {
return null;
}

T data = head.data;
head = head.next;

if (head == null) {
tail = null;
}

return data;
}

public synchronized boolean isEmpty() {
return head == null;
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/thealgorithms/searches/JumpSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@
* Result: Index = 4
*
* <p>
* <b>Example (Simple):</b><br>
* Input: arr = [2, 4, 6, 8, 10], key = 8<br>
* Output: 3
*
* <p>
* <b>Explanation (Easy):</b><br>
* 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.
*
* <p>
* <b>Time Complexity:</b><br>
* - Best-case: O(1) - element found at first position<br>
* - Average: O(√n) - optimal block size reduces jumps<br>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 it
*
* @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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.thealgorithms.datastructures.queues;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class ThreadSafeQueueTest {

@Test
void testQueueOperations() {
ThreadSafeQueue<Integer> queue = new ThreadSafeQueue<>();

assertTrue(queue.isEmpty());

queue.enqueue(1);
queue.enqueue(2);

assertFalse(queue.isEmpty());

assertEquals(1, queue.dequeue());
assertEquals(2, queue.dequeue());

assertTrue(queue.isEmpty());
}

@Test
void testDequeueEmpty() {
ThreadSafeQueue<Integer> queue = new ThreadSafeQueue<>();

assertNull(queue.dequeue());
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading