circular_queue.c

Download
c 208 lines 5.1 KB
  1// circular_queue.c
  2// μ›ν˜• 큐(Circular Queue) κ΅¬ν˜„
  3// λ°°μ—΄μ˜ μ•žλΆ€λΆ„ μž¬μ‚¬μš©μœΌλ‘œ 곡간 νš¨μœ¨μ„± ν–₯상
  4
  5#include <stdio.h>
  6#include <stdbool.h>
  7
  8#define MAX_SIZE 5
  9
 10// μ›ν˜• 큐 ꡬ쑰체
 11typedef struct {
 12    int data[MAX_SIZE];
 13    int front;  // 첫 번째 μš”μ†Œ μœ„μΉ˜
 14    int rear;   // λ§ˆμ§€λ§‰ μš”μ†Œ μœ„μΉ˜
 15    int count;  // ν˜„μž¬ μš”μ†Œ 개수
 16} CircularQueue;
 17
 18// 큐 μ΄ˆκΈ°ν™”
 19void queue_init(CircularQueue *q) {
 20    q->front = 0;
 21    q->rear = -1;
 22    q->count = 0;
 23}
 24
 25// λΉ„μ–΄μžˆλŠ”μ§€ 확인
 26bool queue_isEmpty(CircularQueue *q) {
 27    return q->count == 0;
 28}
 29
 30// 가득 μ°ΌλŠ”μ§€ 확인
 31bool queue_isFull(CircularQueue *q) {
 32    return q->count == MAX_SIZE;
 33}
 34
 35// Enqueue - 뒀에 μš”μ†Œ μΆ”κ°€ (O(1))
 36bool queue_enqueue(CircularQueue *q, int value) {
 37    if (queue_isFull(q)) {
 38        printf("Queue is full!\n");
 39        return false;
 40    }
 41
 42    q->rear = (q->rear + 1) % MAX_SIZE;  // μ›ν˜•μœΌλ‘œ μˆœν™˜
 43    q->data[q->rear] = value;
 44    q->count++;
 45    return true;
 46}
 47
 48// Dequeue - μ•žμ—μ„œ μš”μ†Œ 제거 (O(1))
 49bool queue_dequeue(CircularQueue *q, int *value) {
 50    if (queue_isEmpty(q)) {
 51        printf("Queue is empty!\n");
 52        return false;
 53    }
 54
 55    *value = q->data[q->front];
 56    q->front = (q->front + 1) % MAX_SIZE;  // μ›ν˜•μœΌλ‘œ μˆœν™˜
 57    q->count--;
 58    return true;
 59}
 60
 61// Front - μ•žμ˜ κ°’ 확인 (제거 μ•ˆν•¨)
 62bool queue_front(CircularQueue *q, int *value) {
 63    if (queue_isEmpty(q)) {
 64        return false;
 65    }
 66    *value = q->data[q->front];
 67    return true;
 68}
 69
 70// Rear - λ’€μ˜ κ°’ 확인
 71bool queue_rear(CircularQueue *q, int *value) {
 72    if (queue_isEmpty(q)) {
 73        return false;
 74    }
 75    *value = q->data[q->rear];
 76    return true;
 77}
 78
 79// 큐 좜λ ₯ (frontλΆ€ν„° rearκΉŒμ§€)
 80void queue_print(CircularQueue *q) {
 81    printf("Queue (count=%d): [", q->count);
 82    if (!queue_isEmpty(q)) {
 83        int i = q->front;
 84        for (int c = 0; c < q->count; c++) {
 85            printf("%d", q->data[i]);
 86            if (c < q->count - 1) printf(", ");
 87            i = (i + 1) % MAX_SIZE;
 88        }
 89    }
 90    printf("] (front=%d, rear=%d)\n", q->front, q->rear);
 91}
 92
 93// λ°°μ—΄ μƒνƒœ μ‹œκ°ν™” (λ””λ²„κΉ…μš©)
 94void queue_visualize(CircularQueue *q) {
 95    printf("λ°°μ—΄ μƒνƒœ: [");
 96    for (int i = 0; i < MAX_SIZE; i++) {
 97        if (q->count > 0) {
 98            int start = q->front;
 99            int end = q->rear;
100            bool inRange = false;
101
102            // μ›ν˜• νμ—μ„œ iκ°€ 유효 λ²”μœ„μΈμ§€ 확인
103            if (start <= end) {
104                inRange = (i >= start && i <= end);
105            } else {
106                inRange = (i >= start || i <= end);
107            }
108
109            if (inRange) {
110                printf("%d", q->data[i]);
111            } else {
112                printf("-");
113            }
114        } else {
115            printf("-");
116        }
117        if (i < MAX_SIZE - 1) printf(" ");
118    }
119    printf("]\n");
120
121    // front와 rear μœ„μΉ˜ ν‘œμ‹œ
122    printf("           ");
123    for (int i = 0; i < MAX_SIZE; i++) {
124        if (i == q->front && i == q->rear && q->count > 0) {
125            printf("FR");
126        } else if (i == q->front && q->count > 0) {
127            printf("F ");
128        } else if (i == q->rear && q->count > 0) {
129            printf("R ");
130        } else {
131            printf("  ");
132        }
133    }
134    printf("\n");
135}
136
137// 큐 크기 λ°˜ν™˜
138int queue_size(CircularQueue *q) {
139    return q->count;
140}
141
142// ν…ŒμŠ€νŠΈ μ½”λ“œ
143int main(void) {
144    CircularQueue q;
145    queue_init(&q);
146
147    printf("=== μ›ν˜• 큐 ν…ŒμŠ€νŠΈ ===\n\n");
148
149    // Enqueue ν…ŒμŠ€νŠΈ (큐λ₯Ό 가득 채움)
150    printf("[ 1단계: Enqueue 5개 (큐 가득 μ±„μš°κΈ°) ]\n");
151    for (int i = 1; i <= 5; i++) {
152        printf("Enqueue %d -> ", i * 10);
153        queue_enqueue(&q, i * 10);
154        queue_print(&q);
155        queue_visualize(&q);
156        printf("\n");
157    }
158
159    // Full μƒνƒœμ—μ„œ μΆ”κ°€ μ‹œλ„
160    printf("[ 2단계: Full μƒνƒœμ—μ„œ Enqueue μ‹œλ„ ]\n");
161    printf("Enqueue 60 -> ");
162    queue_enqueue(&q, 60);
163    printf("\n");
164
165    // Dequeue 2개
166    int value;
167    printf("[ 3단계: Dequeue 2개 ]\n");
168    for (int i = 0; i < 2; i++) {
169        queue_dequeue(&q, &value);
170        printf("Dequeued: %d -> ", value);
171        queue_print(&q);
172        queue_visualize(&q);
173        printf("\n");
174    }
175
176    // λ‹€μ‹œ Enqueue (μ›ν˜• νŠΉμ„± 확인)
177    printf("[ 4단계: λ‹€μ‹œ Enqueue 2개 (μ›ν˜• μˆœν™˜ 확인) ]\n");
178    for (int i = 6; i <= 7; i++) {
179        printf("Enqueue %d -> ", i * 10);
180        queue_enqueue(&q, i * 10);
181        queue_print(&q);
182        queue_visualize(&q);
183        printf("\n");
184    }
185
186    // Front와 Rear 확인
187    printf("[ 5단계: Front/Rear 확인 ]\n");
188    int front_val, rear_val;
189    if (queue_front(&q, &front_val) && queue_rear(&q, &rear_val)) {
190        printf("Front κ°’: %d, Rear κ°’: %d\n", front_val, rear_val);
191    }
192    printf("\n");
193
194    // λͺ¨λ“  μš”μ†Œ Dequeue
195    printf("[ 6단계: λͺ¨λ“  μš”μ†Œ Dequeue ]\n");
196    while (queue_dequeue(&q, &value)) {
197        printf("Dequeued: %d -> ", value);
198        queue_print(&q);
199    }
200
201    // Empty μƒνƒœμ—μ„œ 제거 μ‹œλ„
202    printf("\n[ 7단계: Empty μƒνƒœμ—μ„œ Dequeue μ‹œλ„ ]\n");
203    printf("Dequeue -> ");
204    queue_dequeue(&q, &value);
205
206    return 0;
207}