#Queue

큐의 선입선출 구조
스택과 마찬가지로 삽입과 삭제의 위치가 제한적인 자료구조
-큐의 뒤에서는 삽입만 하고, 큐의 앞에서는 삭제만 이루어지는 구조
삽입 : enQueue
삭제 : deQueue

front = rear 상태가 같으면 공백
둘다 -1 이면 처음 구조
front = 가장 첫번째 데이터의 한칸전
rear = 마지막 데이터의 위치


기본 개념과 구조는 이렇게 있다.




public class Queue {
        
        public static int Queue[] = new int[10];
        public static int front = -1;
        public static int rear = -1;
        
        public static void main(String[] args) {
                
                enQueue(1);
                enQueue(2);
                enQueue(3);
                enQueue(4);
                enQueue(5);
                enQueue(6);
                
                System.out.println(deQueue());
                System.out.println(deQueue());
                System.out.println(deQueue());
                
        }
        
        public static boolean isEmpty() {
                return front == rear; //rear와 front 가 같으면 비어있는 상태이다
        }
        
        public static boolean isFull() {
                return rear == Queue.length-1; // Queue 전체 크기보다 -1 작으면 가득찬 상태이다
        }
        
        public static void enQueue(int i) {
                if(isFull()) { //가득 찼는지 우선 확인해야하고
                        System.out.println("에러");
                }else {
                        Queue[++rear] = i; //안찼으면 enqueue 할 수 있다.
                }
        }
        
        public static int Qpeek() {
                return Queue[front+1]; //삭제 하는건 아니고 그냥 값만 가지고 오는 경우
                
        }

        public static int deQueue() {
                if(isEmpty()) {
                        System.out.println("에러");
                }
                return Queue[++front]; //가지고 오고 front를 늘려서 삭제 하는 경우
        }
        
}


'알고리즘' 카테고리의 다른 글

BufferedReader 로 입출력 속도 단축하기  (0) 2019.01.23
후위 표현식 계산하기  (0) 2019.01.22
후위 표현식 만들기 (stack) - java  (0) 2019.01.22

+ Recent posts