diff --git a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java index 259e108a..b43110d4 100644 --- a/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java +++ b/src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java @@ -15,17 +15,14 @@ import java.util.Stack; * * @author sahilb2 (https://www.github.com/sahilb2) */ -class QueueWithStack { - - // Stack to keep track of elements inserted into the queue - private Stack inStack; - // Stack to keep track of elements to be removed next in queue - private Stack outStack; +public class QueueUsingTwoStacks { + private final Stack inStack; + private final Stack outStack; /** * Constructor */ - QueueWithStack() { + public QueueUsingTwoStacks() { this.inStack = new Stack<>(); this.outStack = new Stack<>(); } @@ -94,7 +91,7 @@ class QueueWithStack { * @return true if the inStack is empty. */ public boolean isInStackEmpty() { - return (inStack.size() == 0); + return (inStack.isEmpty()); } /** @@ -103,73 +100,6 @@ class QueueWithStack { * @return true if the outStack is empty. */ public boolean isOutStackEmpty() { - return (outStack.size() == 0); - } -} - -/** - * This class is the example for the Queue class - * - * @author sahilb2 (https://www.github.com/sahilb2) - */ -public final class QueueUsingTwoStacks { - private QueueUsingTwoStacks() { - } - - /** - * Main method - * - * @param args Command line arguments - */ - public static void main(String[] args) { - QueueWithStack myQueue = new QueueWithStack(); - myQueue.insert(1); - System.out.println(myQueue.peekBack()); // Will print 1 - // instack: [(top) 1] - // outStack: [] - myQueue.insert(2); - System.out.println(myQueue.peekBack()); // Will print 2 - // instack: [(top) 2, 1] - // outStack: [] - myQueue.insert(3); - System.out.println(myQueue.peekBack()); // Will print 3 - // instack: [(top) 3, 2, 1] - // outStack: [] - myQueue.insert(4); - System.out.println(myQueue.peekBack()); // Will print 4 - // instack: [(top) 4, 3, 2, 1] - // outStack: [] - - System.out.println(myQueue.isEmpty()); // Will print false - - System.out.println(myQueue.remove()); // Will print 1 - System.out.println((myQueue.isInStackEmpty()) ? "null" : myQueue.peekBack()); // Will print NULL - // instack: [] - // outStack: [(top) 2, 3, 4] - - myQueue.insert(5); - System.out.println(myQueue.peekFront()); // Will print 2 - // instack: [(top) 5] - // outStack: [(top) 2, 3, 4] - - myQueue.remove(); - System.out.println(myQueue.peekFront()); // Will print 3 - // instack: [(top) 5] - // outStack: [(top) 3, 4] - myQueue.remove(); - System.out.println(myQueue.peekFront()); // Will print 4 - // instack: [(top) 5] - // outStack: [(top) 4] - myQueue.remove(); - // instack: [(top) 5] - // outStack: [] - System.out.println(myQueue.peekFront()); // Will print 5 - // instack: [] - // outStack: [(top) 5] - myQueue.remove(); - // instack: [] - // outStack: [] - - System.out.println(myQueue.isEmpty()); // Will print true + return (outStack.isEmpty()); } } diff --git a/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java b/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java new file mode 100644 index 00000000..4901b9c0 --- /dev/null +++ b/src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java @@ -0,0 +1,145 @@ +package com.thealgorithms.others; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.util.EmptyStackException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class QueueUsingTwoStacksTest { + + private QueueUsingTwoStacks queue; + + @BeforeEach + void setUp() { + queue = new QueueUsingTwoStacks(); + } + + @Test + void testIsEmptyInitially() { + assertTrue(queue.isEmpty(), "Queue should be empty initially"); + } + + @Test + void testInsertSingleElement() { + queue.insert(1); + assertFalse(queue.isEmpty(), "Queue should not be empty after inserting an element"); + assertEquals(1, queue.peekFront(), "The front element should be the inserted element"); + } + + @Test + void testRemoveSingleElement() { + queue.insert(1); + assertEquals(1, queue.remove(), "Removing should return the first inserted element"); + assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element"); + } + + @Test + void testRemoveMultipleElements() { + queue.insert(1); + queue.insert(2); + queue.insert(3); + assertEquals(1, queue.remove(), "First removed element should be the first inserted element"); + assertEquals(2, queue.remove(), "Second removed element should be the second inserted element"); + assertEquals(3, queue.remove(), "Third removed element should be the third inserted element"); + assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements"); + } + + @Test + void testPeekFrontWithMultipleElements() { + queue.insert(1); + queue.insert(2); + queue.insert(3); + assertEquals(1, queue.peekFront(), "The front element should be the first inserted element"); + } + + @Test + void testPeekBackWithMultipleElements() { + queue.insert(1); + queue.insert(2); + queue.insert(3); + assertEquals(3, queue.peekBack(), "The back element should be the last inserted element"); + } + + @Test + void testPeekFrontAfterRemovals() { + queue.insert(1); + queue.insert(2); + queue.insert(3); + queue.remove(); + assertEquals(2, queue.peekFront(), "After removing one element, the front should be the second element"); + } + + @Test + void testIsEmptyAfterRemovals() { + queue.insert(1); + queue.insert(2); + queue.remove(); + queue.remove(); + assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements"); + } + + @Test + void testRemoveFromEmptyQueue() { + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::remove, "Removing from an empty queue should throw an exception"); + } + + @Test + void testPeekFrontFromEmptyQueue() { + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekFront, "Peeking front from an empty queue should throw an exception"); + } + + @Test + void testPeekBackFromEmptyQueue() { + org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekBack, "Peeking back from an empty queue should throw an exception"); + } + + @Test + void testIsInStackEmptyInitially() { + assertTrue(queue.isInStackEmpty(), "inStack should be empty initially"); + } + + @Test + void testIsOutStackEmptyInitially() { + assertTrue(queue.isOutStackEmpty(), "outStack should be empty initially"); + } + + @Test + void testIsInStackEmptyAfterInsertion() { + queue.insert(1); + assertFalse(queue.isInStackEmpty(), "inStack should not be empty after an insertion"); + } + + @Test + void testIsOutStackEmptyAfterInsertion() { + queue.insert(1); + assertTrue(queue.isOutStackEmpty(), "outStack should still be empty after an insertion"); + } + + @Test + void testIsOutStackEmptyAfterRemoval() { + queue.insert(1); + queue.remove(); + assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing the only element"); + } + + @Test + void testIsInStackEmptyAfterMultipleRemovals() { + queue.insert(1); + queue.insert(2); + queue.remove(); + queue.remove(); + assertTrue(queue.isInStackEmpty(), "inStack should be empty after removing all elements"); + } + + @Test + void testIsOutStackEmptyAfterMultipleRemovals() { + queue.insert(1); + queue.insert(2); + queue.remove(); + queue.remove(); + assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing all elements"); + } +}