/
004.circularQueueArray.c
61 lines (50 loc) · 1.08 KB
/
004.circularQueueArray.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
// blog.naver.com/ljy9378
// Copyright by 로졔 2019
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
typedef struct {
int key;
//다른 데이터 추가 가능
//char c;
//float f;
} element;
element queue[MAXSIZE];
int rear = 0;
int front = 0;
void queueFull(){
//에러 메시지 출력 후 종료
fprintf(stderr, "queue is full, cannot add element\n");
exit(EXIT_FAILURE);
}
void queueEmpty(){
//에러 메시지 출력 후 종료
fprintf(stderr, "queue is empty, cannot remove element\n");
exit(EXIT_FAILURE);
}
void addQ(element item){
rear = (rear+1) % MAXSIZE;
if(rear == front) queueFull(); //print error and exit
queue[rear] = item;
}
element deleteQ(){
if(rear == front) queueEmpty(); //print error and exit
else {
front = (front+1) % MAXSIZE;
return queue[front];
}
}
int main(){
int i;
element e;
for(i=1; i<=9; i++){
e.key=i;
addQ(e);
}
for(i=1; i<=9; i++){
e = deleteQ();
printf("%d ", e.key);
}
e=deleteQ();
return 0;
}