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
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 5
 
typedef int element;
typedef struct {                 // Å¥ Å¸ÀÔ
    int  front;
    int rear;
    element  data[MAX_QUEUE_SIZE];
} QueueType;
 
// ¿À·ù ÇÔ¼ö
void error(char *message)
{
    fprintf(stderr, "%s\n", message);
    exit(1);
}
 
void init_queue(QueueType *q)
{
    q->rear = -1;
    q->front = -1;
}
void queue_print(QueueType *q)
{
    for (int i = 0; i<MAX_QUEUE_SIZE; i++) {
        if (i <= q->front || i> q->rear)
            printf("   | ");
        else
            printf("%d | ", q->data[i]);
    }
    printf("\n");
}
 
int is_full(QueueType *q)
{
    if (q->rear == MAX_QUEUE_SIZE - 1)
        return 1;
    else
        return 0;
}
 
int is_empty(QueueType *q)
{
    if (q->front == q->rear)
        return 1;
    else
        return 0;
}
 
void enqueue(QueueType *q, int item)
{
    if (is_full(q)) {
        error("Å¥°¡ Æ÷È­»óÅÂÀÔ´Ï´Ù.");
        return;
    }
    q->data[++(q->rear)] = item;
}
 
int dequeue(QueueType *q)
{
    if (is_empty(q)) {
        error("Å¥°¡ °ø¹é»óÅÂÀÔ´Ï´Ù.");
        return -1;
    }
    int item = q->data[++(q->front)];
    return item;
}
 
int main(void)
{
    int item = 0;
    QueueType q;
 
    init_queue(&q);
 
    enqueue(&q, 10); queue_print(&q);
    enqueue(&q, 20); queue_print(&q);
    enqueue(&q, 30); queue_print(&q);
 
    item = dequeue(&q); queue_print(&q);
    item = dequeue(&q); queue_print(&q);
    item = dequeue(&q); queue_print(&q);
    return 0;
}
cs