본문 바로가기
자료구조

Queue 큐

by 코딩개발 2023. 7. 27.
728x90
반응형

Queue

1. FIFO(Fist In First Out)
2. 큐의 앞 부분인 front는 삭제 연산만 수행
3. 큐의 뒷 부분인 rear는 삽입 연산만 수행
4. 그래프 넓이 우선 탐색(BFS)에서 사용

LinkedList를 사용하여 Queue와 LinkedList를 Import

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

Queue<Integer> queue = new LinkedList<>(); //int형 queue 선언, linkedlist 이용
Queue<String> queue = new LinkedList<>(); //String형 queue 선언, linkedlist 이용
Queue<Integer> i3 = new LinkedList<Integer>(Arrays.asList(1, 2, 3)); // 선언과 동시에 초기값 세팅

 


Queue 추가

queue.add(1);
queue.offer(2);

 

  삽입 성공 삽입 실패
add(value) true IllegalStateException
offer(value) true false


Queue 삭제

queue.remove();
queue.poll();

 

Queue가 비어 있을 경우

remove()  NoSuchElement 에러 반환
poll() null 반환

 

queue.peek() // 값을 빼지 않고 첫 번째 값 확인
queue.size() // 큐 사이즈

 

 

예외 발생 값 리턴
add() offer()
remove() poll()

<참고>
https://coding-factory.tistory.com/602
https://goodteacher.tistory.com/112

https://crazykim2.tistory.com/571

728x90
반응형

'자료구조' 카테고리의 다른 글

Stack 스택  (0) 2023.07.26

댓글