큐(queue)는 컴퓨터의 기본적인 자료 구조의 한가지로, 먼저 집어 넣은 데이터가 먼저 나오는 FIFO(First In First Out)구조로 저장하는 형식을 말한다. 영어 단어 queue는 표를 사러 일렬로 늘어선 사람들로 이루어진 줄을 말하기도 하며, 먼저 줄을 선 사람이 먼저 나갈 수 있는 상황을 연상하면 된다.
나중에 집어 넣은 데이터가 먼저 나오는 스택과는 반대되는 개념이다.
// LinkedList implementation of Queue
Queue<String> animal1 = new LinkedList<>();
// Array implementation of Queue
Queue<String> animal2 = new ArrayDeque<>();
// Priority Queue implementation of Queue
Queue<String> animal 3 = new PriorityQueue<>();
import java.util.LinkedList;
class Main {
public static void main(String[] args){
Queue<String> queue = new LinkedList<>();
queue.add("Dog");
queue.add("Cat");
queue.add("Cow");
queue.offer("Chicken");
// add() method with the index parameter
queue.add(1, "Horse");
}
}
import java.util.LinkedList;
class Main {
public static void main(String[] args){
Queue<Integer> queue = new LinkedList<>();
queue.offer(1); // queue에 값 1 추가
queue.offer(2); // queue에 값 2 추가
queue.offer(3); // queue에 값 3 추가
queue.poll(); // queue에 첫번째 값을 반환하고 제거 비어있다면 null
queue.remove(); // queue에 첫번째 값 제거
queue.clear(); // queue 초기화
}
}
import java.util.LinkedList;
class Main {
public static void main(String[] args){
Queue<Integer> queue = new LinkedList<>();
queue.offer(1); // queue에 값 1 추가
queue.offer(2); // queue에 값 2 추가
queue.offer(3); // queue에 값 3 추가
queue.peek(); // queue의 첫번째 값 참조
}
}