Skip to content

Commit 43da361

Browse files
authored
Merge branch 'master' into merge_k_sorted_new_algo
2 parents d116fa9 + 82dee61 commit 43da361

File tree

9 files changed

+474
-174
lines changed

9 files changed

+474
-174
lines changed

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@
202202
* [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java)
203203
* [Queue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java)
204204
* [QueueByTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java)
205+
* [TokenBucket](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/TokenBucket.java)
205206
* stacks
206207
* [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java)
207208
* [ReverseStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java)
@@ -867,7 +868,9 @@
867868
* [PriorityQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/PriorityQueuesTest.java)
868869
* [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java)
869870
* [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java)
871+
* [TokenBucketTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/TokenBucketTest.java)
870872
* stacks
873+
* [NodeStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/NodeStackTest.java)
871874
* [StackArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayListTest.java)
872875
* [StackArrayTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackArrayTest.java)
873876
* [StackOfLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/StackOfLinkedListTest.java)

src/main/java/com/thealgorithms/datastructures/lists/RotateSinglyLinkedLists.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
11
package com.thealgorithms.datastructures.lists;
22

33
/**
4-
* Rotate a list
5-
* @author Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
4+
* The RotateSinglyLinkedLists class provides a method to rotate a singly linked list
5+
* to the right by a specified number of positions.
6+
* <p>
7+
* In a right rotation by `k` steps, each node in the list moves `k` positions to the right.
8+
* Nodes that are rotated off the end of the list are placed back at the beginning.
9+
* </p>
10+
* <p>
11+
* Example:
12+
* Given linked list: 1 -> 2 -> 3 -> 4 -> 5 and k = 2, the output will be:
13+
* 4 -> 5 -> 1 -> 2 -> 3.
14+
* </p>
15+
* <p>
16+
* Edge Cases:
17+
* <ul>
18+
* <li>If the list is empty, returns null.</li>
19+
* <li>If `k` is 0 or a multiple of the list length, the list remains unchanged.</li>
20+
* </ul>
21+
* </p>
22+
* <p>
23+
* Complexity:
24+
* <ul>
25+
* <li>Time Complexity: O(n), where n is the number of nodes in the linked list.</li>
26+
* <li>Space Complexity: O(1), as we only use a constant amount of additional space.</li>
27+
* </ul>
28+
* </p>
29+
*
30+
* Author: Bama Charan Chhandogi (https://github.com/BamaCharanChhandogi)
631
*/
7-
832
public class RotateSinglyLinkedLists {
33+
34+
/**
35+
* Rotates a singly linked list to the right by `k` positions.
36+
*
37+
* @param head The head node of the singly linked list.
38+
* @param k The number of positions to rotate the list to the right.
39+
* @return The head of the rotated linked list.
40+
*/
941
public Node rotateRight(Node head, int k) {
1042
if (head == null || head.next == null || k == 0) {
1143
return head;

src/main/java/com/thealgorithms/datastructures/queues/GenericArrayListQueue.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,46 @@
44
import java.util.List;
55

66
/**
7-
* This class implements a GenericArrayListQueue.
7+
* This class implements a GenericArrayListQueue, a queue data structure that
8+
* holds elements of any type specified at runtime, allowing flexibility in the type
9+
* of elements it stores.
810
*
9-
* A GenericArrayListQueue data structure functions the same as any
10-
* specific-typed queue. The GenericArrayListQueue holds elements of types
11-
* to-be-specified at runtime. The elements that are added first are the first
12-
* to be removed (FIFO). New elements are added to the back/rear of the queue.
11+
* <p>The GenericArrayListQueue operates on a First-In-First-Out (FIFO) basis, where
12+
* elements added first are the first to be removed. New elements are added to the back
13+
* (or rear) of the queue, while removal of elements occurs from the front.
14+
*
15+
* @param <T> The type of elements held in this queue.
1316
*/
1417
public class GenericArrayListQueue<T> {
1518

1619
/**
17-
* The generic List for the queue. T is the generic element type.
20+
* A list that stores the queue's elements in insertion order.
1821
*/
1922
private final List<T> elementList = new ArrayList<>();
2023

2124
/**
2225
* Checks if the queue is empty.
2326
*
24-
* @return True if the queue is empty, false otherwise.
27+
* @return {@code true} if the queue has no elements; {@code false} otherwise.
2528
*/
26-
private boolean isEmpty() {
29+
public boolean isEmpty() {
2730
return elementList.isEmpty();
2831
}
2932

3033
/**
31-
* Returns the element at the front of the queue without removing it.
34+
* Retrieves, but does not remove, the element at the front of the queue.
3235
*
33-
* @return The element at the front of the queue, or null if the queue is empty.
36+
* @return The element at the front of the queue, or {@code null} if the queue is empty.
3437
*/
3538
public T peek() {
3639
return isEmpty() ? null : elementList.getFirst();
3740
}
3841

3942
/**
40-
* Inserts an element of type T to the back of the queue.
43+
* Inserts an element at the back of the queue.
4144
*
42-
* @param element the element to be added to the queue.
43-
* @return True if the element was added successfully.
45+
* @param element The element to be added to the queue.
46+
* @return {@code true} if the element was successfully added.
4447
*/
4548
public boolean add(T element) {
4649
return elementList.add(element);
@@ -49,7 +52,7 @@ public boolean add(T element) {
4952
/**
5053
* Retrieves and removes the element at the front of the queue.
5154
*
52-
* @return The element removed from the front of the queue, or null if the queue is empty.
55+
* @return The element removed from the front of the queue, or {@code null} if the queue is empty.
5356
*/
5457
public T poll() {
5558
return isEmpty() ? null : elementList.removeFirst();
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.thealgorithms.datastructures.queues;
2+
3+
import java.util.concurrent.TimeUnit;
4+
5+
/**
6+
* TokenBucket implements a token bucket rate limiter algorithm.
7+
* This class is used to control the rate of requests in a distributed system.
8+
* It allows a certain number of requests (tokens) to be processed in a time frame,
9+
* based on the defined refill rate.
10+
*
11+
* Applications: Computer networks, API rate limiting, distributed systems, etc.
12+
*
13+
* @author Hardvan
14+
*/
15+
public final class TokenBucket {
16+
private final int maxTokens;
17+
private final int refillRate; // tokens per second
18+
private int tokens;
19+
private long lastRefill; // Timestamp in nanoseconds
20+
21+
/**
22+
* Constructs a TokenBucket instance.
23+
*
24+
* @param maxTokens Maximum number of tokens the bucket can hold.
25+
* @param refillRate The rate at which tokens are refilled (tokens per second).
26+
*/
27+
public TokenBucket(int maxTokens, int refillRate) {
28+
this.maxTokens = maxTokens;
29+
this.refillRate = refillRate;
30+
this.tokens = maxTokens;
31+
this.lastRefill = System.nanoTime();
32+
}
33+
34+
/**
35+
* Attempts to allow a request based on the available tokens.
36+
* If a token is available, it decrements the token count and allows the request.
37+
* Otherwise, the request is denied.
38+
*
39+
* @return true if the request is allowed, false if the request is denied.
40+
*/
41+
public synchronized boolean allowRequest() {
42+
refillTokens();
43+
if (tokens > 0) {
44+
tokens--;
45+
return true;
46+
}
47+
return false;
48+
}
49+
50+
/**
51+
* Refills the tokens based on the time elapsed since the last refill.
52+
* The number of tokens to be added is calculated based on the elapsed time
53+
* and the refill rate, ensuring the total does not exceed maxTokens.
54+
*/
55+
private void refillTokens() {
56+
long now = System.nanoTime();
57+
long tokensToAdd = (now - lastRefill) / TimeUnit.SECONDS.toNanos(1) * refillRate;
58+
tokens = Math.min(maxTokens, tokens + (int) tokensToAdd);
59+
lastRefill = now;
60+
}
61+
}

0 commit comments

Comments
 (0)