-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayQueue.java
71 lines (59 loc) · 1.45 KB
/
ArrayQueue.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
63
64
65
66
67
68
69
70
71
package structure;
public class ArrayQueue {
private static final int SIZE = 10;
private int[] array;
private int head;
private int tail;
public ArrayQueue() {
array = new int[SIZE];
}
public void offer(int data) {
if (isPossibleSize()) {
array[tail++] = data;
} else {
increaseArray();
array[tail++] = data;
}
}
public int poll() {
if (tail == 0) {
System.out.println("Queue is empty");
return -1;
}
int data = array[head];
// 위치 조정
for (int i = 1; i < tail; i++) {
array[i - 1] = array[i];
}
tail--;
return data;
}
public int size() {
return tail;
}
protected void increaseArray() {
int[] temp = array;
// 기존 사이즈에서 5 증가
array = new int[temp.length + SIZE];
for (int i = 0; i < temp.length; i++) {
array[i] = temp[i];
}
}
protected boolean isPossibleSize() {
if (array.length <= tail) {
return false;
} else {
return true;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{ ");
for (int i = 0; i < tail; i++) {
sb.append(array[i]).append(" ");
}
sb.append("}");
return sb.toString();
}
}