-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQueue.java
More file actions
47 lines (38 loc) · 1.04 KB
/
MyQueue.java
File metadata and controls
47 lines (38 loc) · 1.04 KB
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
package linear;
import java.util.ArrayDeque;
import java.util.Deque;
class MyQueue {
private Deque<Integer> input;
private Deque<Integer> output;
// 큐 초기화
public MyQueue() {
input= new ArrayDeque<>();
output = new ArrayDeque<>();
}
// 큐의 뒤에 x추가
public void push(int x) {
input.push(x);
}
// 큐의 가장 앞 요소를 제거 후 값 반환
public int pop() {
moveInputToOutput();
return output.pop();
}
// 큐의 가장 앞 요소의 값만 반환
public int peek() {
moveInputToOutput();
return output.peek();
}
// 큐가 비어있는지 확인
public boolean empty() {
return input.isEmpty() && output.isEmpty();
}
// output이 비어있으면, input의 모든 요소를 output으로 옮김 -> 요소 순서 뒤집기.
private void moveInputToOutput() {
if (output.isEmpty()) {
while (!input.isEmpty()) {
output.push(input.pop());
}
}
}
}