배열 큐의 문제점
front/rear를 사용하는 선형 큐의 문제
원형 큐
index = (index + 1) % capacity;
class MyCircularQueue {
private int[] arr;
private int head; // 가장 앞 요소 인덱스
private int tail; // 가장 뒷 요소
private int size; // 현재 들어있는 요소 개수.head== tail 일 때 처리(비어있음, 하나만 있음, 꽉참)
private int capacity; // 배열의 전체 크기
public MyCircularQueue(int k) {
this.capacity = k;
this.arr = new int[k];
this.head = 0;
this.tail = -1;
this.size = 0;
}
// 원소 추가, tail
public boolean enQueue(int value) {
if (isFull()) return false;
tail = (tail + 1) % capacity;
arr[tail] = value;
size++;
return true;
}
// 원소 제거, gead
public boolean deQueue() {
if (isEmpty()) return false;
head = (head + 1) % capacity;
size--;
return true;
}
public int Front() {
if (isEmpty()) return -1;
return arr[head];
}
public int Rear() {
if (isEmpty()) return -1;
return arr[tail];
}
public boolean isEmpty() {
return size == 0;
}
public boolean isFull() {
return size == capacity;
}
}