2019. 3. 3. 22:41
# 큐(Queue)란?
- 먼저 삽입된 데이터가 가장 먼저 제거되는 선입선출(FIFO - First In First Out) 형태의 자료구조
# 큐(Queue) API
* Object element(): 저장된 요소를 불러옴
* boolean offer(Object o ): Queue에 객체 저장 ( true:성공, false:실패) 반환
* Object peek(): 저장된 객체를 반환 / 없을경우 Null 을 반환
* Object poll(): 객체를 꺼내온다 / 꺼낸객체는 사라짐
# 큐(Queue) 클래스 사용법
LinkedList 클래스로 Queue를 인스턴스화하여 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | import java.util.LinkedList; import java.util.Queue; public class Main { static Queue <Integer> q; public static void main(String[] args) { // queue는 LinkedList 클래스로 선언한다. q = new LinkedList<Integer>(); // queue 초기화 q.clear(); // queue에 데이터 1 삽입 q.add(1); // queue에 데이터 5 삽입 q.add(5); // queue의 size 출력 System.out.println("queue size: " +q.size()); // queue에서 데이터 확인 및 제거 System.out.println("data(pool): " +q.poll()); // queue에서 데이터 확인 System.out.println("data(peek): " +q.peek()); // queue에서 데이터 확인 및 제거 System.out.println("data(pool): " +q.poll()); // queue에 데이터가 존재하는지 확인 if (q.isEmpty()) { System.out.println("queue is empty"); } } } /*********** 결과 *************** queue size: 2 data(pool): 1 data(peek): 5 data(pool): 5 queue is empty */ | cs |
# 큐(Queue) 구현 - 배열 사용
# 문제
주어진 N(2<= N <=100)개의 수를 순서대로 Queue에 넣은 후 하나씩 꺼내 화면에 출력
# 입력
2
5
1 2 3 4 5
5
5 4 2 3 1
# 출력
#1 1 2 3 4 5
#2 5 4 2 3 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import java.io.FileInputStream; import java.util.Scanner; class Main2 { static final int MAX_N = 100; static int front; static int rear; static int queue[] = new int[MAX_N]; static void queueInit() { front = 0; rear = 0; } static boolean queueIsEmpty() { return (front == rear); } static boolean queueIsFull() { if ((front + 1) % MAX_N == rear) { return true; } else { return false; } } static boolean queueEnqueue(int value) { if (queueIsFull()) { System.out.print("queue is full!"); return false; } queue[front] = value; front++; if (front >= MAX_N) { front = MAX_N; } return true; } static Integer queueDequeue() { if (queueIsEmpty()) { System.out.print("queue is empty!"); return null; } Integer value = new Integer(queue[rear]); rear++; if (rear >= MAX_N) { rear = MAX_N; } return value; } public static void main(String arg[]) throws Exception { //System.setIn(new FileInputStream("c:\\boj\\sample.txt")); Scanner sc = new Scanner(System.in); int T = sc.nextInt(); for (int test_case = 1; test_case <= T; test_case++) { int N = sc.nextInt(); queueInit(); for (int i = 0; i < N; i++) { int value = sc.nextInt(); queueEnqueue(value); } System.out.print("#" + test_case + " "); while (!queueIsEmpty()) { Integer value = queueDequeue(); if (value != null) { System.out.print(value.intValue() + " "); } } System.out.println(); } sc.close(); } } | cs |
# 참고사이트
https://hyeonstorage.tistory.com/263
https://swalloow.tistory.com/32
https://gamjatwigim.tistory.com/72
'자료구조' 카테고리의 다른 글
이진검색트리 (Binary Search Tree) (정리중,,,,,,) (0) | 2019.03.04 |
---|---|
스택(stack) 개념 및 사용법 정리 (0) | 2019.03.03 |
배열 리스트(ArrayList) 개념 및 사용법 (0) | 2019.03.03 |
자바 배열 사용법 정리 (0) | 2019.03.03 |