Skip to content

Commit 35cb01f

Browse files
authored
Merge branch 'master' into refactor/CountWords
2 parents 3565d51 + 6b7a1fd commit 35cb01f

File tree

2 files changed

+151
-76
lines changed

2 files changed

+151
-76
lines changed

src/main/java/com/thealgorithms/others/QueueUsingTwoStacks.java

+6-76
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,14 @@
1515
*
1616
* @author sahilb2 (https://www.github.com/sahilb2)
1717
*/
18-
class QueueWithStack {
19-
20-
// Stack to keep track of elements inserted into the queue
21-
private Stack<Object> inStack;
22-
// Stack to keep track of elements to be removed next in queue
23-
private Stack<Object> outStack;
18+
public class QueueUsingTwoStacks {
19+
private final Stack<Object> inStack;
20+
private final Stack<Object> outStack;
2421

2522
/**
2623
* Constructor
2724
*/
28-
QueueWithStack() {
25+
public QueueUsingTwoStacks() {
2926
this.inStack = new Stack<>();
3027
this.outStack = new Stack<>();
3128
}
@@ -94,7 +91,7 @@ public boolean isEmpty() {
9491
* @return true if the inStack is empty.
9592
*/
9693
public boolean isInStackEmpty() {
97-
return (inStack.size() == 0);
94+
return (inStack.isEmpty());
9895
}
9996

10097
/**
@@ -103,73 +100,6 @@ public boolean isInStackEmpty() {
103100
* @return true if the outStack is empty.
104101
*/
105102
public boolean isOutStackEmpty() {
106-
return (outStack.size() == 0);
107-
}
108-
}
109-
110-
/**
111-
* This class is the example for the Queue class
112-
*
113-
* @author sahilb2 (https://www.github.com/sahilb2)
114-
*/
115-
public final class QueueUsingTwoStacks {
116-
private QueueUsingTwoStacks() {
117-
}
118-
119-
/**
120-
* Main method
121-
*
122-
* @param args Command line arguments
123-
*/
124-
public static void main(String[] args) {
125-
QueueWithStack myQueue = new QueueWithStack();
126-
myQueue.insert(1);
127-
System.out.println(myQueue.peekBack()); // Will print 1
128-
// instack: [(top) 1]
129-
// outStack: []
130-
myQueue.insert(2);
131-
System.out.println(myQueue.peekBack()); // Will print 2
132-
// instack: [(top) 2, 1]
133-
// outStack: []
134-
myQueue.insert(3);
135-
System.out.println(myQueue.peekBack()); // Will print 3
136-
// instack: [(top) 3, 2, 1]
137-
// outStack: []
138-
myQueue.insert(4);
139-
System.out.println(myQueue.peekBack()); // Will print 4
140-
// instack: [(top) 4, 3, 2, 1]
141-
// outStack: []
142-
143-
System.out.println(myQueue.isEmpty()); // Will print false
144-
145-
System.out.println(myQueue.remove()); // Will print 1
146-
System.out.println((myQueue.isInStackEmpty()) ? "null" : myQueue.peekBack()); // Will print NULL
147-
// instack: []
148-
// outStack: [(top) 2, 3, 4]
149-
150-
myQueue.insert(5);
151-
System.out.println(myQueue.peekFront()); // Will print 2
152-
// instack: [(top) 5]
153-
// outStack: [(top) 2, 3, 4]
154-
155-
myQueue.remove();
156-
System.out.println(myQueue.peekFront()); // Will print 3
157-
// instack: [(top) 5]
158-
// outStack: [(top) 3, 4]
159-
myQueue.remove();
160-
System.out.println(myQueue.peekFront()); // Will print 4
161-
// instack: [(top) 5]
162-
// outStack: [(top) 4]
163-
myQueue.remove();
164-
// instack: [(top) 5]
165-
// outStack: []
166-
System.out.println(myQueue.peekFront()); // Will print 5
167-
// instack: []
168-
// outStack: [(top) 5]
169-
myQueue.remove();
170-
// instack: []
171-
// outStack: []
172-
173-
System.out.println(myQueue.isEmpty()); // Will print true
103+
return (outStack.isEmpty());
174104
}
175105
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package com.thealgorithms.others;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertFalse;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.EmptyStackException;
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
class QueueUsingTwoStacksTest {
12+
13+
private QueueUsingTwoStacks queue;
14+
15+
@BeforeEach
16+
void setUp() {
17+
queue = new QueueUsingTwoStacks();
18+
}
19+
20+
@Test
21+
void testIsEmptyInitially() {
22+
assertTrue(queue.isEmpty(), "Queue should be empty initially");
23+
}
24+
25+
@Test
26+
void testInsertSingleElement() {
27+
queue.insert(1);
28+
assertFalse(queue.isEmpty(), "Queue should not be empty after inserting an element");
29+
assertEquals(1, queue.peekFront(), "The front element should be the inserted element");
30+
}
31+
32+
@Test
33+
void testRemoveSingleElement() {
34+
queue.insert(1);
35+
assertEquals(1, queue.remove(), "Removing should return the first inserted element");
36+
assertTrue(queue.isEmpty(), "Queue should be empty after removing the only element");
37+
}
38+
39+
@Test
40+
void testRemoveMultipleElements() {
41+
queue.insert(1);
42+
queue.insert(2);
43+
queue.insert(3);
44+
assertEquals(1, queue.remove(), "First removed element should be the first inserted element");
45+
assertEquals(2, queue.remove(), "Second removed element should be the second inserted element");
46+
assertEquals(3, queue.remove(), "Third removed element should be the third inserted element");
47+
assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
48+
}
49+
50+
@Test
51+
void testPeekFrontWithMultipleElements() {
52+
queue.insert(1);
53+
queue.insert(2);
54+
queue.insert(3);
55+
assertEquals(1, queue.peekFront(), "The front element should be the first inserted element");
56+
}
57+
58+
@Test
59+
void testPeekBackWithMultipleElements() {
60+
queue.insert(1);
61+
queue.insert(2);
62+
queue.insert(3);
63+
assertEquals(3, queue.peekBack(), "The back element should be the last inserted element");
64+
}
65+
66+
@Test
67+
void testPeekFrontAfterRemovals() {
68+
queue.insert(1);
69+
queue.insert(2);
70+
queue.insert(3);
71+
queue.remove();
72+
assertEquals(2, queue.peekFront(), "After removing one element, the front should be the second element");
73+
}
74+
75+
@Test
76+
void testIsEmptyAfterRemovals() {
77+
queue.insert(1);
78+
queue.insert(2);
79+
queue.remove();
80+
queue.remove();
81+
assertTrue(queue.isEmpty(), "Queue should be empty after removing all elements");
82+
}
83+
84+
@Test
85+
void testRemoveFromEmptyQueue() {
86+
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::remove, "Removing from an empty queue should throw an exception");
87+
}
88+
89+
@Test
90+
void testPeekFrontFromEmptyQueue() {
91+
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekFront, "Peeking front from an empty queue should throw an exception");
92+
}
93+
94+
@Test
95+
void testPeekBackFromEmptyQueue() {
96+
org.junit.jupiter.api.Assertions.assertThrows(EmptyStackException.class, queue::peekBack, "Peeking back from an empty queue should throw an exception");
97+
}
98+
99+
@Test
100+
void testIsInStackEmptyInitially() {
101+
assertTrue(queue.isInStackEmpty(), "inStack should be empty initially");
102+
}
103+
104+
@Test
105+
void testIsOutStackEmptyInitially() {
106+
assertTrue(queue.isOutStackEmpty(), "outStack should be empty initially");
107+
}
108+
109+
@Test
110+
void testIsInStackEmptyAfterInsertion() {
111+
queue.insert(1);
112+
assertFalse(queue.isInStackEmpty(), "inStack should not be empty after an insertion");
113+
}
114+
115+
@Test
116+
void testIsOutStackEmptyAfterInsertion() {
117+
queue.insert(1);
118+
assertTrue(queue.isOutStackEmpty(), "outStack should still be empty after an insertion");
119+
}
120+
121+
@Test
122+
void testIsOutStackEmptyAfterRemoval() {
123+
queue.insert(1);
124+
queue.remove();
125+
assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing the only element");
126+
}
127+
128+
@Test
129+
void testIsInStackEmptyAfterMultipleRemovals() {
130+
queue.insert(1);
131+
queue.insert(2);
132+
queue.remove();
133+
queue.remove();
134+
assertTrue(queue.isInStackEmpty(), "inStack should be empty after removing all elements");
135+
}
136+
137+
@Test
138+
void testIsOutStackEmptyAfterMultipleRemovals() {
139+
queue.insert(1);
140+
queue.insert(2);
141+
queue.remove();
142+
queue.remove();
143+
assertTrue(queue.isOutStackEmpty(), "outStack should be empty after removing all elements");
144+
}
145+
}

0 commit comments

Comments
 (0)