forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Efficient_Dequeue.java
70 lines (57 loc) · 1.39 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
62
63
64
65
66
67
68
69
70
package Stacks;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by Last on 10/19/2016.
*/
public class Pop_Efficient {
Queue<Integer> q1 = new LinkedList<>(), q2 = new LinkedList<>();
public Integer pop(){
if(q1.isEmpty())
return Integer.MIN_VALUE;
return q1.remove();
}
public void push(int data){
if(!q1.isEmpty()){
int size = q1.size();
for (int i = 0; i < size; i++) {
q2.add(q1.remove());
}
q1.add(data);
for (int i = 0; i < size; i++) {
q1.add(q2.remove());
}
}
else{
q1.add(data);
}
}
public void printStack(){
for (int x: q1) {
System.out.println(x);
}
}
public static void main(String[] args) {
Pop_Efficient pop_efficient = new Pop_Efficient();
pop_efficient.push(10);
pop_efficient.push(20);
pop_efficient.push(30);
pop_efficient.push(40);
pop_efficient.push(50);
pop_efficient.pop();
pop_efficient.push(60);
pop_efficient.push(70);
pop_efficient.pop();
pop_efficient.push(80);
pop_efficient.push(90);
pop_efficient.printStack();
}
}
//Output
// 90
// 80
// 60
// 40
// 30
// 20
// 10