Skip to content

Commit faaf078

Browse files
committed
Add StackUsingTwoQueues.java new algorithm
1 parent f34fe4d commit faaf078

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
*/
12+
public class StackUsingTwoQueues {
13+
14+
private Queue<Integer> mainQueue;
15+
private Queue<Integer> tempQueue;
16+
17+
/**
18+
* Constructs an empty stack using two queues.
19+
*/
20+
public StackUsingTwoQueues() {
21+
mainQueue = new LinkedList<>();
22+
tempQueue = new LinkedList<>();
23+
}
24+
25+
/**
26+
* Pushes an element onto the top of the stack.
27+
* The newly pushed element becomes the top of the stack.
28+
*
29+
* @param item The element to be pushed onto the stack.
30+
*/
31+
public void push(int item) {
32+
tempQueue.add(item);
33+
34+
// Move all elements from the mainQueue to tempQueue to maintain LIFO order
35+
while (!mainQueue.isEmpty()) {
36+
tempQueue.add(mainQueue.remove());
37+
}
38+
39+
// Swap the names of the two queues
40+
Queue<Integer> swap = mainQueue;
41+
mainQueue = tempQueue;
42+
tempQueue = swap; // tempQueue is now empty
43+
}
44+
45+
/**
46+
* Removes and returns the element at the top of the stack.
47+
* Throws an exception if the stack is empty.
48+
*
49+
* @return The element at the top of the stack.
50+
* @throws NoSuchElementException if the stack is empty.
51+
*/
52+
public int pop() {
53+
if (mainQueue.isEmpty()) {
54+
throw new NoSuchElementException("Stack is empty");
55+
}
56+
return mainQueue.remove();
57+
}
58+
59+
/**
60+
* Returns the element at the top of the stack without removing it.
61+
* Returns null if the stack is empty.
62+
*
63+
* @return The element at the top of the stack, or null if the stack is empty.
64+
*/
65+
public Integer peek() {
66+
if (mainQueue.isEmpty()) {
67+
return null;
68+
}
69+
return mainQueue.peek();
70+
}
71+
72+
/**
73+
* Returns true if the stack is empty.
74+
*
75+
* @return true if the stack is empty; false otherwise.
76+
*/
77+
public boolean isEmpty() {
78+
return mainQueue.isEmpty();
79+
}
80+
81+
/**
82+
* Returns the number of elements in the stack.
83+
*
84+
* @return The size of the stack.
85+
*/
86+
public int size() {
87+
return mainQueue.size();
88+
}
89+
}
Lines changed: 70 additions & 0 deletions
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)