forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP23_1.java
More file actions
32 lines (26 loc) · 796 Bytes
/
P23_1.java
File metadata and controls
32 lines (26 loc) · 796 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
package ch09;
import java.util.LinkedList;
import java.util.Queue;
public class P23_1 {
// 큐 변수, 구현체는 LinkedList로 선언
Queue<Integer> queue = new LinkedList<>();
public void push(int x) {
// 엘리먼트 삽입
queue.add(x);
// 맨 앞에 두는 상태로 전체 재정렬
for (int i = 1; i < queue.size(); i++)
queue.add(queue.remove());
}
public int pop() {
// 재정렬한 상태이므로 큐 연산으로 추출
return queue.remove();
}
public int top() {
// 재정렬한 상태이므로 큐 연산으로 조회
return queue.peek();
}
public boolean empty() {
// 크기를 비교해 비어있는지 확인
return queue.size() == 0;
}
}