-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathCircularQueue.c
78 lines (74 loc) · 1.34 KB
/
CircularQueue.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
76
77
78
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int size;
int front;
int rear;
int *Q;
};
void create(struct queue *q, int x)
{
q->size = x;
q->front = q->rear = -1;
q->Q = (int *)malloc(sizeof(int) * q->size);
}
void enQueue(struct queue *q, int val)
{
if ((q->rear + 1) % q->size == q->front)
{
printf("The Queue is full\n");
}
else
{
q->Q[(q->rear + 1) % q->size] = val;
q->rear = (q->rear + 1) % q->size;
}
}
int deQueue(struct queue *q)
{
int x = -1;
if (q->front == q->rear)
{
printf("Queue is empty\n");
}
else
{
x = q->Q[q->front];
q->front = (q->front + 1) % q->size;
}
return x;
}
void display(struct queue q)
{
int i = (q.front + 1) % q.size;
while (i != (q.rear + 1) % q.size)
{
printf("%d ", q.Q[i]);
i = (i + 1) % q.size;
}
printf("\n");
}
// void display(struct queue q)
// {
// int i=q.front+1;
// do
// {
// printf("%d ",q.Q[i]);
// i=(i+1)%q.size;
// }while(i!=(q.rear+1)%q.size);
// printf("\n");
// }
int main()
{
struct queue q;
create(&q, 6);
enQueue(&q, 5);
enQueue(&q, 4);
enQueue(&q, 3);
enQueue(&q, 2);
display(q);
deQueue(&q);
display(q);
return 0;
}