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