forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Efficient_Enqueue.java
57 lines (56 loc) · 1.14 KB
/
Efficient_Enqueue.java
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
import java.util.Stack;
public class EfficientEnqueue {
Stack < Integer > primaryStack = new Stack < > ();
Stack < Integer > secondaryStack = new Stack < > ();
public void enqueue(int element)
{
primaryStack.push(element);
}
public int dequeue()
{
if (this.size() == 0)
{
System.out.print("UnderFlow ");
return Integer.MIN_VALUE;
}
if (secondaryStack.empty())
{
while (!primaryStack.empty())
{
secondaryStack.push(primaryStack.pop());
}
}
return secondaryStack.pop();
}
public boolean isEmpty()
{
return primaryStack.empty() && secondaryStack.empty();
}
public int size()
{
return primaryStack.size() + secondaryStack.size();
}
public static void main(String args[]) {
EfficientEnqueue q1 = new EfficientEnqueue();
for (int i = 0; i < 10; i++)
{
q1.enqueue(i);
}
while (!q1.isEmpty())
{
System.out.println(q1.dequeue());
}
}
}
/*OUTPUT
0
1
2
3
4
5
6
7
8
9
*/