Algorithm/Concept
큐 (Queue)
shbada
2022. 3. 7. 19:35
728x90
반응형
큐
- 선입선출 : 먼저 들어 온 데이터가 먼저 나가는 형식
예제코드
import java.util.LinkedList;
import java.util.Queue;
/**
* Queue
*/
public class M2_Queue {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
queue.offer(5); // push 5
queue.offer(3); // push 3
queue.offer(2); // push 2
queue.poll(); // pop 5
queue.offer(1); // push 1
queue.poll(); // pop 3
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
}
}
queue.offer(5)
5 |
queue.offer(3)
3 | 5 |
queue.offer(2)
2 | 3 | 5 |
queue.poll()
2 | 3 |
queue.offer(1)
1 | 2 | 3 |
queue.poll()
1 | 2 |
반응형