-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathImplementStackUsingQueues.java
62 lines (47 loc) · 1.37 KB
/
ImplementStackUsingQueues.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
package problems.leetcode;
import java.util.ArrayDeque;
import java.util.Deque;
// https://leetcode.com/problems/implement-stack-using-queues/submissions/
public class ImplementStackUsingQueues {
private static class MyStack {
// push 1
// push 2
// push 3
// peek -> 3
// pop 3
// push 4
// peek -> 4
// 1 -> 2 -> 3 -> 4
private Deque<Integer> queue1 = new ArrayDeque<>();
private Deque<Integer> queue2 = new ArrayDeque<>();
public MyStack() {
}
public void push(int x) {
queue1.addLast(x);
}
public int pop() {
while (queue1.size() > 1) {
queue2.addLast(queue1.removeFirst());
}
int i = queue1.removeFirst();
Deque<Integer> temp = queue1;
queue1 = queue2;
queue2 = temp;
return i;
}
public int top() {
int i = pop();
push(i);
return i;
}
public boolean empty() {
return queue1.isEmpty();
}
}
public static void main(String[] args) {
MyStack stack = new MyStack();
stack.push(1);
System.out.println(stack.pop());
System.out.println(stack.empty());
}
}