위의 프론트와 리어를 head 와 tail 로 구현함.
public class queue {
private int[] array;
private int head;
private int tail;
public queue(int size){
array = new int[size];
head = -1;
tail = -1;
}
public void enQueue(int number){
// 큐가 꽉 찼는지 확인을 함~
if (tail == array.length -1)
throw new RuntimeException("queue is full");
// 들어올 때마다 한칸씩 밀어줌 ~~~
array[++tail] =number;
}
public int deQueue(){
// 큐가 비어있는지 확인함~
if (tail == -1)
throw new RuntimeException("there is no data in queue");
// 큐가빠질 때마다 한칸 씩 밀어줌~
int temp = array[++head];
array[head] = -1;
if (head == tail){
tail = -1;
head = -1;
}
return temp;
}}