Post

Queue (큐)

1. 큐(Queue)

  • 선입선출 (First In First Out, FIFO) 자료구조
    • 먼저 들어온 데이터가 먼저 나가는 구조
  • 입력 순서대로 데이터 처리가 필요할 때 사용
    • 프린터 출력 대기열, BFS(Breath-First Search) 등

Queue

2. 큐 기본 구조

  • 선입선출 구조를 따름
  • 기본적으로 데이터 추가, 꺼내기, 큐 공간 확인 동작으로 이루어짐

Untitled

Untitled 1

Untitled 2

(실습 : 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
// 선형 자료구조 - 큐

import java.util.LinkedList;
import java.util.Queue;

public class Main {
    public static void main(String[] args) {
        Queue queue = new LinkedList<>();

        queue.add(1);
        queue.add(2);
        queue.add(3);
        queue.add(4);
        queue.add(5);
        System.out.println(queue);

        System.out.println(queue.poll());
        System.out.println(queue);

        System.out.println(queue.poll());
        System.out.println(queue);

        System.out.println(queue.peek());
        System.out.println(queue);

        System.out.println(queue.contains(3));
        System.out.println(queue.size());
        System.out.println(queue.isEmpty());

        queue.clear();
        System.out.println(queue);
        System.out.println(queue.poll());

    }
}

Untitled 3

This post is licensed under CC BY 4.0 by the author.