Skip to content

refactor: QueueUsingTwoStacks #5427

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

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
82 changes: 6 additions & 76 deletions src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@
*
* @author sahilb2 (https://www.github.com/sahilb2)
*/
class QueueWithStack {

// Stack to keep track of elements inserted into the queue
private Stack<Object> inStack;
// Stack to keep track of elements to be removed next in queue
private Stack<Object> outStack;
public class QueueUsingTwoStacks {
private final Stack<Object> inStack;
private final Stack<Object> outStack;

/**
* Constructor
*/
QueueWithStack() {
public QueueUsingTwoStacks() {
this.inStack = new Stack<>();
this.outStack = new Stack<>();
}
Expand Down Expand Up @@ -94,7 +91,7 @@ public boolean isEmpty() {
* @return true if the inStack is empty.
*/
public boolean isInStackEmpty() {
return (inStack.size() == 0);
return (inStack.isEmpty());
}

/**
Expand All @@ -103,73 +100,6 @@ public boolean isInStackEmpty() {
* @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());
}
}
145 changes: 145 additions & 0 deletions src/test/java/com/thealgorithms/others/QueueUsingTwoStacksTest.java
Original file line number Diff line number Diff line change
@@ -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");
}
}