반응형
자료구조 중 큐입니다.
큐는 FIFO(First In First Out)구조로 먼저 들어가면 먼저 나온다 입니다.
스택에선 Push, Pop이었지만 큐에서는 Enqueue, Dequeue로 사용합니다.
큐에는 선형으로 되어있는 선형 큐와 원형 큐가 있습니다.
(이부분은 추후에 작성토록 하겠습니다.)
배열로 큐입니다.
#include <stdio.h>
#define MAX_QUEUE_SIZE 100
int queue[MAX_QUEUE_SIZE];
int front = 0;
int rear = 0;
void enqueue(int item) {
if ((rear + 1) % MAX_QUEUE_SIZE == front) {
printf("Queue is full\n");
} else {
rear = (rear + 1) % MAX_QUEUE_SIZE;
queue[rear] = item;
}
}
int dequeue() {
int item;
if (front == rear) {
printf("Queue is empty\n");
return -1;
} else {
front = (front + 1) % MAX_QUEUE_SIZE;
item = queue[front];
return item;
}
}
int main() {
enqueue(1);
enqueue(2);
enqueue(3);
printf("%d\n", dequeue());
printf("%d\n", dequeue());
printf("%d\n", dequeue());
return 0;
}
반응형
'프로그래밍 > C' 카테고리의 다른 글
C언어 스택 (0) | 2023.01.29 |
---|---|
[프로그래머스] 숫자 찾기 - C (0) | 2023.01.11 |
[프로그래머스] 약수 구하기 - C (0) | 2023.01.11 |
[프로그래머스] 7의 개수 - C (0) | 2023.01.11 |