자료구조&알고리즘
[자료구조] 큐 (Queue)
순늘봄
2023. 10. 23. 23:52
큐 (Queue):
스택과는 다르게 큐는 선입선출의 구조를 가지고 있다. 즉, 먼저 들어온 자료가 먼저 나간다 (First In First Out, FIFO)는 것이다. 은행 창구나 카페를 떠올리면 이해하기가 쉽다. 먼저 온 손님이 음료를 먼저 받고 나가는 것을 예시로 들면 좋을 것 같다.
삭제 연산이 수행되는 곳은 프론트 (front), 삽입 연산이 이루어지는 곳은 리어 (rear)이다.
· Enqueue: 리어 (rear)에서 이루어지는 삽입 연산
· Dequeue: 프론트 (Front)에서 이루어지는 삭제 연산
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue <int> q;
q.push(7);
q.push(5);
q.push(4);
q.pop();
q.push(6);
q.pop();
while (!q.empty()) {
cout << q.front() << " ";
q.pop();
}
return 0;
}