-
Notifications
You must be signed in to change notification settings - Fork 21.1k
Add simple thread-safe queue implementation #7390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
prashantpiyush1111
wants to merge
18
commits into
TheAlgorithms:master
Choose a base branch
from
prashantpiyush1111:feature/thread-safe-queue
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+239
−0
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ca21a27
docs: add edge cases to JumpSearch documentation
prashantpiyush1111 4c7ac35
fix: remove trailing whitespace (checkstyle)
prashantpiyush1111 56a087d
docs: add simple example and beginner-friendly explanation to JumpSearch
prashantpiyush1111 41020e5
feat: add StackUsingLinkedList implementation with test cases
prashantpiyush1111 db4e0c3
style: apply clang-format to fix formatting issues
prashantpiyush1111 43553e1
style: fix checkstyle issues (newline and imports)
prashantpiyush1111 bedd1ce
style: fix newline at end of StackUsingLinkedList
prashantpiyush1111 ca2cdfc
style: fix formatting to satisfy clang-format and checkstyle
prashantpiyush1111 9cd54ee
Merge branch 'master' into master
prashantpiyush1111 51fff7f
feat: add simple thread-safe queue using synchronized
prashantpiyush1111 b2199c9
style: fix formatting issues (clang-format)
prashantpiyush1111 f4b7991
test: add proper unit tests for ThreadSafeQueue
prashantpiyush1111 4e9244c
fix: resolve checkstyle issues (newline, imports, inner assignment)
prashantpiyush1111 0f1bc22
style: fix indentation (tabs removed)
prashantpiyush1111 0ba8aba
style: fix indentation fully (no tabs)
prashantpiyush1111 d4415ff
style: fix test file indentation (final)
prashantpiyush1111 3293397
style: final fix test formatting and imports
prashantpiyush1111 6ee6b34
Merge branch 'master' into feature/thread-safe-queue
prashantpiyush1111 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
src/main/java/com/thealgorithms/datastructures/queues/ThreadSafeQueue.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/java/com/thealgorithms/stacks/StackUsingLinkedList.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
|
|
||
| /** | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this is a test code review |
||
| * 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; | ||
| } | ||
| } | ||
35 changes: 35 additions & 0 deletions
35
src/test/java/com/thealgorithms/datastructures/queues/ThreadSafeQueueTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()); | ||
| } | ||
| } |
75 changes: 75 additions & 0 deletions
75
src/test/java/com/thealgorithms/stacks/StackUsingLinkedListTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just a practice/test code review, and I am a very beginner in this.