반응형
컬랙션 Queue(줄을 서서 기다리다) : 데이터를 일시적으로 쌓아두기 위한 자료구조
- Stack과 다르게 FIFO형태를 가짐 쉽게 말해서 가장 먼저 저장된(psuh)데이터가 가장 먼저 나오게(pop) 되는 구조
- Queue를 생성할 때 linkedList를 활용하여 생성
큐 - Array deQueue, Linked List<>
Queue 주요 메서드
add(value) - 삽입 | 반환값 : 삽입 성공 시 true / 실패 시 Exception발생 |
offer(E,e)-삽입 | 반환 값(boolean): 삽입 성공 시 true / 실패 시 false 반환 |
poll() -삭제 | 환 값(삭제된 value의 자료형): 삭제된 value / 공백 큐이면 null 반환 |
remove(value),remove()-삭제 | 반환 값(boolean): 큐에 해당 value가 존재하면 해당 값 삭제 후 true / 존재하지 않으면 false 반환 |
element() | 반환 값(큐 head에 위치한 value의 자료형): 큐 head에 위치한 value / 공백 큐이면 Exception("NoSuhElementException") 발생 |
peek() | 반환 값(큐 head에 위치한 value의 자료형): 큐 head에 위치한 value / 공백 큐이면 null 반환 |
size() | 반환 값(int): 큐 사이즈 반환 |
LinkedList<String> que =new LinkedList<>();
// 큐에 요소 추가
que.add("홍길동");
que.add("이순신 ");
que.add("강감찬");
// 큐에 있는 요소 반환
System.out.println(que.peek());
System.out.println(que);
// 큐에 있는 요소 반환+ 삭제
System.out.println(que.poll());
System.out.println(que);
//요소 삭제
System.out.println(que.remove(0));
//크기 출력
System.out.println(que.size());
que.add("광개토 대왕");
que.add("장수왕");
que.add("세종 대왕");
System.out.println("전체 출력");
for(String s : que){
System.out.println(s);
}
System.out.println("맨 앞에 있는 요소 : "+que.element());
//offer 메서드
que.offer("정조");
System.out.println(que);
System.out.println("peek 사용 =>"+que.peek());//peek 사용 =>강감찬
System.out.println("element 사용 =>"+que.element()); //element 사용 =>강감찬
System.out.println(que.get(2)); //장수왕
System.out.println(que);// [강감찬, 광개토 대왕, 장수왕, 세종 대왕, 정조]
}
}
반응형
'자료구조' 카테고리의 다른 글
버블 정렬(bubbleSort) (0) | 2024.08.27 |
---|---|
스택(Stack) (0) | 2024.08.19 |
CollectionFrameWork - ArrayList, Linked list (0) | 2024.08.05 |
배열(Array) (0) | 2024.07.22 |