한쪽 끝에서 원소를 넣고 반대쪽 끝에서 원소를 뺄 수 있는 FIFO(First in First Out) 형식의 자료구조
class Queue {
private Integer array[];
private int front;
private int rear;
private int size;
public Queue(int size) {
this.size = size;
this.array = new Integer[size];
this.front = -1;
this.rear = -1;
}
void add(int data) {
rear++;
if (rear >= size) {
throw new ArrayIndexOutOfBoundsException();
}
array[rear] = data;
if (front == -1) {
front = rear;
}
}
int remove() {
if (front >= size || front < 0) {
throw new ArrayIndexOutOfBoundsException();
}
return array[front++];
}
int peek() {
if (isEmpty()) {
throw new ArrayIndexOutOfBoundsException();
} else {
return array[front];
}
}
boolean isEmpty() {
return front == rear;
}
}
데이터가 입력된 시간 순서대로 처리해야 할 필요가 있는 상황에 이용