728x90
반응형
1. 큐 (Queue)
- 선입선출 (First In First Out; FIFO) 자료구조
- 먼저 들어온 데이터가 먼저 나가는 구조
- 입력 순서대로 데이터 처리가 필요할 때 사용
- 프린터 출력 대기열, BFS (Breath-First Search) 등
2. 큐 기본 구조
- 선입선출 구조를 따름
- 기본적으로 데이터 추가/꺼내기/큐 공간 확인 동작으로 이루어짐
3. 큐 기본 연산
- 데이터 추가 (Enqueue)
- 큐에 데이터 추가
- 큐에 데이터 추가
- 데이터 꺼내기 (Dequeue)
- 큐에서 데이터 꺼내기
- 큐에서 데이터 꺼내기
4. 코드
// Queue는 인터페이스라서 바로 객체 생성 불가능
Queue queue = new LinkedList();
queue.add(1);
queue.add(2);
queue.add(3);
queue.add(4);
queue.add(5);
System.out.println(queue); // [1, 2, 3, 4, 5]
queue.poll(); // 1
System.out.println(queue); // [2, 3, 4, 5]
queue.poll(); // 2
System.out.println(queue); // [3, 4, 5]
queue.peek(); // 3
System.out.println(queue); // [3, 4, 5]
System.out.println(queue.contains(3)); // true
System.out.println(queue.size()); // 3
System.out.println(queue.isEmpty()); // false
queue.clear();
System.out.println(queue); // []
System.out.println(queue.poll()); // null
728x90
반응형
'CS > 자료구조' 카테고리의 다른 글
[자료구조] 해시 테이블 (Hash Table) (0) | 2024.08.17 |
---|---|
[자료구조] 데크 (Deque) (0) | 2024.08.17 |
[자료구조] 스택 (Stack) (0) | 2024.08.17 |
[자료구조] 원형 연결 리스트(Circular Linked List) 구현 (0) | 2024.08.17 |
[자료구조] 양방향 연결 리스트(Doubly Linked List) 구현 (0) | 2024.08.17 |