forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Efficient_Dequeue.java
61 lines (58 loc) · 1.15 KB
/
Efficient_Dequeue.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
58
59
60
61
import java.util.Stack;
public class EfficientDequeue {
Stack < Integer > primaryStack = new Stack < > ();
Stack < Integer > secondaryStack = new Stack < > ();
public void enqueue(int element)
{
while (!primaryStack.isEmpty())
{
secondaryStack.push(primaryStack.pop());
}
secondaryStack.push(element);
while (!secondaryStack.isEmpty())
{
primaryStack.push(secondaryStack.pop());
}
}
public int dequeue()
{
if(primaryStack.size()==0)
{
System.out.print("UnderFlow ");
return Integer.MIN_VALUE;
}
return primaryStack.pop();
}
public boolean isEmpty()
{
return primaryStack.empty();
}
public int size()
{
return primaryStack.size();
}
public static void main(String args[]) {
EfficientDequeue q1 = new EfficientDequeue();
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
*/