-
Notifications
You must be signed in to change notification settings - Fork 0
/
44_CircularQueue_using_array.c
72 lines (61 loc) · 1.17 KB
/
44_CircularQueue_using_array.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
#include<stdio.h>
#include<stdlib.h>
typedef struct CircularQueue{
int size, front, rear;
int * arr;
}queue;
int isEmpty(queue* q){
if(q->rear==q->front){
return 1;
}
else{
return 0;
}
}
int isFull(queue* q){
if((q->rear+1)%q->size==q->front){
return 1;
}
else{
return 0;
}
}
void enqueue(queue * q, int val){
if(isFull(q)){
printf("The Queue is OverFlow!\n");
}
else{
printf("Enqued element: %d\n", val);
q->rear = (q->rear+1)%q->size;
q->arr[q->rear] = val;
}
}
int dequeue(queue * q){
int val = -1;
if(isEmpty(q)){
printf("The Queue is UnderFlow!\n");
}
else{
q->front = (q->front+1)%q->size;
val = q->arr[q->front];
}
return val;
}
int main(){
queue q;
q.size = 5;
q.front = q.rear = 0;
q.arr = (int*)malloc(q.size*sizeof(int));
enqueue(&q, 12);
enqueue(&q, 25);
enqueue(&q, 32);
enqueue(&q, 41);
printf("Dequed element: %d\n", dequeue(&q));
if(isEmpty(&q)){
printf("Queue is empty\n");
}
if(isFull(&q)){
printf("Queue is full\n");
}
return 0;
}