-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueSolution.java
More file actions
44 lines (33 loc) · 949 Bytes
/
QueueSolution.java
File metadata and controls
44 lines (33 loc) · 949 Bytes
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
package queue;
import java.util.Stack;
public class QueueSolution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.push(node);
stack2.clear();
for (int i = stack1.size() - 1; i >= 0; i--) {
stack2.push(stack1.get(i));
}
}
public int pop() {
int result = stack2.pop();
stack1.clear();
for (int i = stack2.size() - 1; i >= 0; i--) {
stack1.push(stack2.get(i));
}
return result;
}
public static void main(String[] args) {
QueueSolution solution = new QueueSolution();
for (int i = 0; i < 9; i++) {
solution.push(i);
}
solution.pop();
solution.push(11);
for (int i = 0; i < 9; i++) {
int a = solution.pop();
System.out.print(" " + a);
}
}
}