Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
* [LinkedQueue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java)
* [PriorityQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/PriorityQueues.java)
* [Queue](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/Queue.java)
* [QueueByTwoStacks](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/queues/QueueByTwoStacks.java)
* stacks
* [NodeStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java)
* [ReverseStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java)
Expand Down Expand Up @@ -731,6 +732,7 @@
* [GenericArrayListQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/GenericArrayListQueueTest.java)
* [LinkedQueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/LinkedQueueTest.java)
* [PriorityQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/PriorityQueuesTest.java)
* [QueueByTwoStacksTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueByTwoStacksTest.java)
* [QueueTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/queues/QueueTest.java)
* stacks
* [LinkedListStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/stacks/LinkedListStackTest.java)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package com.thealgorithms.datastructures.queues;

import java.util.NoSuchElementException;
import java.util.Stack;

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

private final Stack<T> enqueueStk;
private final Stack<T> dequeueStk;

/**
* Constructor that initializes two empty stacks for the queue.
* The `enqueueStk` is used to push elements when enqueuing, and
* the `dequeueStk` is used to pop elements when dequeuing.
*/
public QueueByTwoStacks() {
enqueueStk = new Stack<>();
dequeueStk = new Stack<>();
}

/**
* Adds an element to the end of the queue. This method pushes the element
* onto the `enqueueStk`.
*
* @param item The element to be added to the queue.
*/
public void put(T item) {
enqueueStk.push(item);
}

/**
* Removes and returns the element at the front of the queue.
* If `dequeueStk` is empty, it transfers all elements from
* `enqueueStk` to `dequeueStk` to maintain the correct FIFO
* (First-In-First-Out) order before popping.
*
* @return The element at the front of the queue.
* @throws NoSuchElementException If the queue is empty.
*/
public T get() {
if (dequeueStk.isEmpty()) {
while (!enqueueStk.isEmpty()) {
dequeueStk.push(enqueueStk.pop());
}
}
if (dequeueStk.isEmpty()) {
throw new NoSuchElementException("Queue is empty");
}
return dequeueStk.pop();
}

/**
* Returns the total number of elements currently in the queue.
* This is the sum of the sizes of both stacks.
*
* @return The number of elements in the queue.
*/
public int size() {
return enqueueStk.size() + dequeueStk.size();
}

/**
* Returns a string representation of the queue, showing the elements
* in the correct order (from front to back).
* The `dequeueStk` is first cloned, and then all elements from the
* `enqueueStk` are added to the cloned stack in reverse order to
* represent the queue accurately.
*
* @return A string representation of the queue.
*/
@Override
public String toString() {
Stack<T> tempStack = (Stack<T>) dequeueStk.clone();
while (!enqueueStk.isEmpty()) {
tempStack.push(enqueueStk.pop());
}
return "Queue(" + tempStack + ")";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.thealgorithms.datastructures.queues;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.util.NoSuchElementException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class QueueByTwoStacksTest {

private QueueByTwoStacks<Integer> queue;

@BeforeEach
public void setUp() {
queue = new QueueByTwoStacks<>();
}

@Test
public void testEmptyQueue() {
assertEquals(0, queue.size());
}

@Test
public void testEnqueue() {
queue.put(10);
queue.put(20);
assertEquals(2, queue.size());
}

@Test
public void testDequeue() {
queue.put(10);
queue.put(20);
queue.put(30);
assertEquals(10, queue.get()); // First item out
assertEquals(20, queue.get()); // Second item out
assertEquals(30, queue.get()); // Third item out
}

@Test
public void testInterleavedOperations() {
queue.put(10);
queue.put(20);
assertEquals(10, queue.get()); // Dequeue first item
queue.put(30);
assertEquals(20, queue.get()); // Dequeue second item
assertEquals(30, queue.get()); // Dequeue third item
}

@Test
public void testQueueSize() {
assertEquals(0, queue.size());
queue.put(1);
assertEquals(1, queue.size());
queue.put(2);
queue.put(3);
assertEquals(3, queue.size());
queue.get();
assertEquals(2, queue.size());
}

@Test
public void testEmptyQueueException() {
assertThrows(NoSuchElementException.class, () -> {
queue.get(); // Attempting to dequeue from empty queue
});
}
}