Skip to content

Commit bb06d89

Browse files
committed
Improve documentation
1 parent 445895c commit bb06d89

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

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

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,48 @@
33
import java.util.Stack;
44
import java.util.NoSuchElementException;
55

6+
/**
7+
* A queue implementation using two stacks. This class provides methods to
8+
* enqueue (add) elements to the end of the queue and dequeue (remove)
9+
* elements from the front, while utilizing two internal stacks to manage
10+
* the order of elements.
11+
*
12+
* @param <T> The type of elements held in this queue.
13+
*/
614
public class QueueByTwoStacks<T> {
715

8-
private Stack<T> enqueueStk;
9-
private Stack<T> dequeueStk;
16+
private final Stack<T> enqueueStk;
17+
private final Stack<T> dequeueStk;
1018

11-
// Constructor initializes stacks and optionally fills the queue from an iterable
19+
/**
20+
* Constructor that initializes two empty stacks for the queue.
21+
* The `enqueueStk` is used to push elements when enqueuing, and
22+
* the `dequeueStk` is used to pop elements when dequeuing.
23+
*/
1224
public QueueByTwoStacks() {
1325
enqueueStk = new Stack<>();
1426
dequeueStk = new Stack<>();
1527
}
1628

17-
// Add an item to the queue
29+
/**
30+
* Adds an element to the end of the queue. This method pushes the element
31+
* onto the `enqueueStk`.
32+
*
33+
* @param item The element to be added to the queue.
34+
*/
1835
public void put(T item) {
1936
enqueueStk.push(item);
2037
}
2138

22-
// Remove and return the first item from the queue
39+
/**
40+
* Removes and returns the element at the front of the queue.
41+
* If `dequeueStk` is empty, it transfers all elements from
42+
* `enqueueStk` to `dequeueStk` to maintain the correct FIFO
43+
* (First-In-First-Out) order before popping.
44+
*
45+
* @return The element at the front of the queue.
46+
* @throws NoSuchElementException If the queue is empty.
47+
*/
2348
public T get() {
2449
if (dequeueStk.isEmpty()) {
2550
while (!enqueueStk.isEmpty()) {
@@ -32,12 +57,25 @@ public T get() {
3257
return dequeueStk.pop();
3358
}
3459

35-
// Get the number of elements in the queue
60+
/**
61+
* Returns the total number of elements currently in the queue.
62+
* This is the sum of the sizes of both stacks.
63+
*
64+
* @return The number of elements in the queue.
65+
*/
3666
public int size() {
3767
return enqueueStk.size() + dequeueStk.size();
3868
}
3969

40-
// Return string representation of the queue
70+
/**
71+
* Returns a string representation of the queue, showing the elements
72+
* in the correct order (from front to back).
73+
* The `dequeueStk` is first cloned, and then all elements from the
74+
* `enqueueStk` are added to the cloned stack in reverse order to
75+
* represent the queue accurately.
76+
*
77+
* @return A string representation of the queue.
78+
*/
4179
@Override
4280
public String toString() {
4381
Stack<T> tempStack = (Stack<T>) dequeueStk.clone();

0 commit comments

Comments
 (0)