-
Notifications
You must be signed in to change notification settings - Fork 0
/
배열을 이용한 원형 큐의 구현.c
75 lines (71 loc) · 1.42 KB
/
배열을 이용한 원형 큐의 구현.c
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
#include<stdlib.h>
#include <stdio.h>
#define MAX_Q_SIZE 5
typedef int element;
typedef struct{
int rear;
int front;
element data[MAX_Q_SIZE];
}Queue;
void initQueue(Queue* q){
q->front = q->rear = 0;
}
int isEmpty(Queue* q)
{
return q->front == q->rear;
}
int isFull(Queue* q)
{
return (q->rear + 1)% MAX_Q_SIZE == q->front;
}
void enQueue(Queue* q, element item)
{
if (isFull(&q)) printf("큐가 포화상태입니다.\n");
else
{
q->data[(++q->rear) % MAX_Q_SIZE] = item;
}//코드작성
}
element deQueue(Queue* q){
if(isEmpty(&q)) printf("큐는 공백상태입니다.\n");
else
{
return q->data[(++q->front) % MAX_Q_SIZE];
}//코드작성
}
void printQueue(Queue* q){
int i;
printf("QUEUE(front=%d rear=%d) = ",q->front,q->rear);
if (isEmpty(&q)) {
printf("큐는 공백상태입니다.\n");
return;
}
i = q->front;
do{
i = (i+1)% (MAX_Q_SIZE);
printf("%d | ",q->data[i]);
if(i == q->rear) break;
}while(i!=q->front);
printf("\n");
}
int main() {
Queue q;
element item;
initQueue(&q);
printf("--데이터 추가 단계--\n");
while (!isFull(&q)){
printf("정수를 입력하시오.");
scanf("%d",&item);
enQueue(&q,item);
printQueue(&q);
}
printf("큐는 포화상태입니다.\n\n");
printf("--데이터 삭제 단계--\n");
while(!isEmpty(&q)){
item = deQueue(&q);
printf("꺼내진 정수 : %d\n",item);
printQueue(&q);
}
printf("큐는 공백상태입니다.\n\n");
return 0;
}