Skip to content

Commit f55fa22

Browse files
Added Stack using Queue implementation in Java with test cases
1 parent c691b31 commit f55fa22

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Implementation of Stack using two Queues.
3+
*
4+
* A Stack follows LIFO (Last In First Out) order.
5+
* We can simulate stack behavior using two queues (q1 and q2).
6+
*
7+
* Approach:
8+
* - For every push operation:
9+
* 1. Add element to q2.
10+
* 2. Move all elements from q1 to q2.
11+
* 3. Swap q1 and q2.
12+
* - For pop operation:
13+
* - Remove the front element from q1.
14+
* - For top operation:
15+
* - Return the front element from q1 without removing it.
16+
*
17+
* Time Complexity:
18+
* push(x): O(n)
19+
* pop(): O(1)
20+
* top(): O(1)
21+
*
22+
* Space Complexity: O(n)
23+
*
24+
* Author: Pradyumn Pratap Singh (Strange)
25+
* For: Hacktoberfest / Open Source Contribution
26+
*/
27+
28+
import java.util.LinkedList;
29+
import java.util.Queue;
30+
31+
public class StackUsingQueue {
32+
33+
private Queue<Integer> q1 = new LinkedList<>();
34+
private Queue<Integer> q2 = new LinkedList<>();
35+
36+
/**
37+
* Push an element onto the stack.
38+
*
39+
* @param x element to be pushed
40+
*/
41+
public void push(int x) {
42+
// Step 1: Add new element to q2
43+
q2.add(x);
44+
45+
// Step 2: Move all elements from q1 to q2
46+
while (!q1.isEmpty()) {
47+
q2.add(q1.poll());
48+
}
49+
50+
// Step 3: Swap the references of q1 and q2
51+
Queue<Integer> temp = q1;
52+
q1 = q2;
53+
q2 = temp;
54+
}
55+
56+
/**
57+
* Pop (remove) the top element from the stack.
58+
*
59+
* @return the popped element, or -1 if stack is empty
60+
*/
61+
public int pop() {
62+
if (q1.isEmpty()) {
63+
System.out.println("Stack Underflow!");
64+
return -1;
65+
}
66+
return q1.poll();
67+
}
68+
69+
/**
70+
* Get the top element of the stack.
71+
*
72+
* @return the top element, or -1 if stack is empty
73+
*/
74+
public int top() {
75+
if (q1.isEmpty()) {
76+
System.out.println("Stack is empty!");
77+
return -1;
78+
}
79+
return q1.peek();
80+
}
81+
82+
/**
83+
* Check if the stack is empty.
84+
*
85+
* @return true if stack is empty, false otherwise
86+
*/
87+
public boolean isEmpty() {
88+
return q1.isEmpty();
89+
}
90+
91+
// ---------------- TEST CASES ----------------
92+
public static void main(String[] args) {
93+
StackUsingQueue stack = new StackUsingQueue();
94+
95+
System.out.println("Pushing elements: 10, 20, 30");
96+
stack.push(10);
97+
stack.push(20);
98+
stack.push(30);
99+
100+
System.out.println("Top element: " + stack.top()); // 30
101+
System.out.println("Pop element: " + stack.pop()); // 30
102+
System.out.println("Top after pop: " + stack.top()); // 20
103+
System.out.println("Is stack empty? " + stack.isEmpty()); // false
104+
105+
stack.pop();
106+
stack.pop();
107+
System.out.println("Pop on empty stack: " + stack.pop()); // -1 (Underflow)
108+
System.out.println("Is stack empty? " + stack.isEmpty()); // true
109+
}
110+
}

0 commit comments

Comments
 (0)