-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueBook.java
More file actions
54 lines (38 loc) · 1.28 KB
/
QueueBook.java
File metadata and controls
54 lines (38 loc) · 1.28 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
48
49
50
51
52
53
54
package java_algo_book;
import java.util.EmptyStackException;
public class QueueBook {
private int max; // 큐 용량
private int num; // 현재 데이터 수
private final int[] queue; // 큐 본체
public class EmptyQueueException extends RuntimeException {}
public class OverflowQueueException extends RuntimeException {}
public QueueBook(int capacity) {
max = capacity;
num = 0;
try {
queue = new int[capacity];
} catch (OutOfMemoryError e) {
max = 0;
throw e;
}
}
// 복잡도 O(1)
public int enqueue(int addElem) {
if (num >= max) throw new OverflowQueueException();
return queue[num++] = addElem;
}
// 복잡도 O(n)
public int dequeue() {
if (num <= 0) throw new EmptyStackException();
int firstElem = queue[0];
for (int i = (num - 1); i > 0; i--) {
queue[i - 1] = queue[i];
}
return firstElem;
}
// 배열 요소를 앞 쪽으로 이동하지 않는 Ring Buffer 자료구조임
// 링 버퍼로 큐 만들기
public static void main(String[] args) {
// Queue - 데이터를 일시적으로 쌓아놓는 데이터 자료구조, FIFO First In First Out
}
}