Skip to content

Commit d3bd287

Browse files
authored
Add StackUsingTwoQueues algorithm (#5625)
1 parent 136e0e2 commit d3bd287

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@
569569
* [PrefixToInfix](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/PrefixToInfix.java)
570570
* [SortStack](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/SortStack.java)
571571
* [StackPostfixNotation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackPostfixNotation.java)
572+
* [StackUsingTwoQueues](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/stacks/StackUsingTwoQueues.java)
572573
* strings
573574
* [AhoCorasick](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/AhoCorasick.java)
574575
* [Alphabetical](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/strings/Alphabetical.java)
@@ -1032,6 +1033,7 @@
10321033
* [PrefixToInfixTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/PrefixToInfixTest.java)
10331034
* [SortStackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/SortStackTest.java)
10341035
* [StackPostfixNotationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackPostfixNotationTest.java)
1036+
* [StackUsingTwoQueuesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/stacks/StackUsingTwoQueuesTest.java)
10351037
* strings
10361038
* [AhoCorasickTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/AhoCorasickTest.java)
10371039
* [AlphabeticalTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/strings/AlphabeticalTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.thealgorithms.stacks;
2+
3+
import java.util.LinkedList;
4+
import java.util.NoSuchElementException;
5+
import java.util.Queue;
6+
7+
/**
8+
* A class that implements a stack using two queues.
9+
* This approach ensures that the stack's LIFO (Last In, First Out) behavior
10+
* is maintained by utilizing two queues for storage.
11+
* The mainQueue is used to store the elements of the stack, while the tempQueue
12+
* is used to temporarily store elements during the push operation.
13+
*/
14+
public class StackUsingTwoQueues {
15+
16+
private Queue<Integer> mainQueue;
17+
private Queue<Integer> tempQueue;
18+
19+
/**
20+
* Constructs an empty stack using two queues.
21+
*/
22+
public StackUsingTwoQueues() {
23+
mainQueue = new LinkedList<>();
24+
tempQueue = new LinkedList<>();
25+
}
26+
27+
/**
28+
* Pushes an element onto the top of the stack.
29+
* The newly pushed element becomes the top of the stack.
30+
*
31+
* @param item The element to be pushed onto the stack.
32+
*/
33+
public void push(int item) {
34+
tempQueue.add(item);
35+
36+
// Move all elements from the mainQueue to tempQueue to maintain LIFO order
37+
while (!mainQueue.isEmpty()) {
38+
tempQueue.add(mainQueue.remove());
39+
}
40+
41+
// Swap the names of the two queues
42+
Queue<Integer> swap = mainQueue;
43+
mainQueue = tempQueue;
44+
tempQueue = swap; // tempQueue is now empty
45+
}
46+
47+
/**
48+
* Removes and returns the element at the top of the stack.
49+
* Throws an exception if the stack is empty.
50+
*
51+
* @return The element at the top of the stack.
52+
* @throws NoSuchElementException if the stack is empty.
53+
*/
54+
public int pop() {
55+
if (mainQueue.isEmpty()) {
56+
throw new NoSuchElementException("Stack is empty");
57+
}
58+
return mainQueue.remove();
59+
}
60+
61+
/**
62+
* Returns the element at the top of the stack without removing it.
63+
* Returns null if the stack is empty.
64+
*
65+
* @return The element at the top of the stack, or null if the stack is empty.
66+
*/
67+
public Integer peek() {
68+
if (mainQueue.isEmpty()) {
69+
return null;
70+
}
71+
return mainQueue.peek();
72+
}
73+
74+
/**
75+
* Returns true if the stack is empty.
76+
*
77+
* @return true if the stack is empty; false otherwise.
78+
*/
79+
public boolean isEmpty() {
80+
return mainQueue.isEmpty();
81+
}
82+
83+
/**
84+
* Returns the number of elements in the stack.
85+
*
86+
* @return The size of the stack.
87+
*/
88+
public int size() {
89+
return mainQueue.size();
90+
}
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.thealgorithms.stacks;
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.assertNull;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
8+
import org.junit.jupiter.api.BeforeEach;
9+
import org.junit.jupiter.api.Test;
10+
11+
public class StackUsingTwoQueuesTest {
12+
13+
private StackUsingTwoQueues stack;
14+
15+
@BeforeEach
16+
public void setUp() {
17+
stack = new StackUsingTwoQueues();
18+
}
19+
20+
@Test
21+
public void testPushAndPeek() {
22+
stack.push(1);
23+
stack.push(2);
24+
stack.push(3);
25+
assertEquals(3, stack.peek());
26+
}
27+
28+
@Test
29+
public void testPop() {
30+
stack.push(1);
31+
stack.push(2);
32+
stack.push(3);
33+
assertEquals(3, stack.pop());
34+
assertEquals(2, stack.pop());
35+
assertEquals(1, stack.pop());
36+
}
37+
38+
@Test
39+
public void testPeek() {
40+
stack.push(10);
41+
stack.push(20);
42+
assertEquals(20, stack.peek());
43+
stack.pop();
44+
assertEquals(10, stack.peek());
45+
}
46+
47+
@Test
48+
public void testIsEmpty() {
49+
assertTrue(stack.isEmpty());
50+
stack.push(1);
51+
assertFalse(stack.isEmpty());
52+
stack.pop();
53+
assertTrue(stack.isEmpty());
54+
}
55+
56+
@Test
57+
public void testSize() {
58+
assertEquals(0, stack.size());
59+
stack.push(1);
60+
stack.push(2);
61+
assertEquals(2, stack.size());
62+
stack.pop();
63+
assertEquals(1, stack.size());
64+
}
65+
66+
@Test
67+
public void testPeekEmptyStack() {
68+
assertNull(stack.peek());
69+
}
70+
}

0 commit comments

Comments
 (0)