[Java/자료구조] Queue(큐) 이란?

doyeon kim·2022년 6월 14일
0
  • 스택과 다르게 선입선출방식(First in First Out) 자료구조
  • 스택과는 다르게 한쪽 끝에서 삽입(rear) 작업이, 다른 쪽 끝에서 삭제(front) 작업이 양쪽으로 이루어진다.
  • 이때 삭제연산만 수행되는 곳을 프론트(front),
  • 삽입연산만 이루어지는 곳을 리어(rear)로 정하여 각각의 연산작업만 수행된다.
  • 이때, 큐의 리어에서 이루어지는 삽입연산을 인큐(enQueue) / 프론트에서 이루어지는 삭제연산을 디큐(dnQueue) 라고 부른다.
  • 줄 선 순서대로 수행이 이뤄지는것이 대표적인 예



*자주쓰는 함수

Queue<String> queue = new LinkedList<>();
//값 추가		
queue.add("1");
queue.offer("2");

queue.poll(); //if queue is empty, it returns null.
queue.remove(); //remove, if queue is empty, but it throws NullException
queue.clear(); //초기화
queue.peek(); //return first value in queue.

queue.forEach(q -> System.out.println(q)); //1,2

[참고자료]
https://brightwon.tistory.com/8
https://devuna.tistory.com/22
https://coding-factory.tistory.com/602

profile
아직은 개발이 재밌음

0개의 댓글