1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
 
// ===== ¿øÇüÅ¥ Äڵ堽ÃÀÛ ======
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct { // Å¥ Å¸ÀÔ
    element  data[MAX_QUEUE_SIZE];
    int  front, rear;
} QueueType;
 
// ¿À·ù ÇÔ¼ö
void error(char *message)
{
    fprintf(stderr, "%s\n", message);
    exit(1);
}
 
// °ø¹é »óÅ °ËÃâ ÇÔ¼ö
void init_queue(QueueType *q)
{
    q->front = q->rear = 0;
}
 
// °ø¹é »óÅ °ËÃâ ÇÔ¼ö
int is_empty(QueueType *q)
{
    return (q->front == q->rear);
}
 
// Æ÷È­ »óÅ °ËÃâ ÇÔ¼ö
int is_full(QueueType *q)
{
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}
 
// ¿øÇüÅ¥ Ãâ·Â ÇÔ¼ö
void queue_print(QueueType *q)
{
    printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) {
            int i = q->front;
            do {
                i = (i + 1) % (MAX_QUEUE_SIZE);
                printf("%d | ", q->data[i]);
                if (i == q->rear)
                    break;
            } while (i != q->front);
        }
    printf("\n");
}
 
// »ðÀÔ ÇÔ¼ö
void enqueue(QueueType *q, element item)
{
    if (is_full(q))
        error("Å¥°¡ Æ÷È­»óÅÂÀÔ´Ï´Ù");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
    q->data[q->rear] = item;
}
 
// »èÁ¦ ÇÔ¼ö
element dequeue(QueueType *q)
{
    if (is_empty(q))
        error("Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù");
    q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    return q->data[q->front];
}
 
// »èÁ¦ ÇÔ¼ö
element peek(QueueType *q)
{
    if (is_empty(q))
        error("Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE];
}
// ===== ¿øÇüÅ¥ Äڵ堳¡ ======
 
int main(void)
{
    QueueType queue;
    int element;
 
    init_queue(&queue);
    srand(time(NULL));
 
    for(int i=0;i<100; i++){
        if (rand() % 5 == 0) {    // 5·Î ³ª´©¾î ¶³¾îÁö¸é 
            enqueue(&queue, rand()%100);
        }
        queue_print(&queue);
        if (rand() % 10 == 0) {    // 10·Î ³ª´©¾î ¶³¾îÁö¸é 
            int data = dequeue(&queue);
        }
        queue_print(&queue);
    }
    return 0;
}
cs