JAVA를 이용한 QUEUE 구현

김상범·2021년 1월 18일
0

1. 큐 구조

  • 줄을 세우는 행위와 유사
  • 가장 먼저 넣은 데이터를 가장 먼저꺼낼 수 있음
  • FIFO (FIRST IN FIRST OUT)

2. 용어

  • enqueue : 데이터를 큐에 삽입
  • dequeue : 데이터를 큐에서 꺼내는 기능

3. 파생 종류

3.1 LIFOQueue (LAST IN FIRST OUT)

3.2 PriorityQueue(우선순위 큐) → 데이터 마다 우선순위를 정해서 우선순위대로 추출

4. 큐의 용도

  • 멀티 태스킹을 위한 프로세스 스케쥴링 방식을 구현하기위해 많이 사용

5. 구현

위의 프론트와 리어를 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;
    }}
profile
아기개발자

0개의 댓글