-
Notifications
You must be signed in to change notification settings - Fork 0
/
Circular queue.c
62 lines (62 loc) · 1.11 KB
/
Circular queue.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
struct Queue
{
int size;
int front;
int rear;
int *Q;
};
void create(struct Queue *q, int size)
{
q->size = size;
q->front = q->rear = 0;
q->Q = (int *)malloc(q->size * sizeof(int));
}
void enqueue(struct Queue *q, int x)
{
if ((q->rear + 1) % q->size == q->front)
printf("Queue is Full");
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = x;
}
}
int dequeue(struct Queue *q)
{
int x = -1;
if (q->front == q->rear)
printf("Queue is Empty\n");
else
{
q->front = (q->front + 1) % q->size;
x = q->Q[q->front];
}
return x;
}
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, 5);
enqueue(&q, 10);
enqueue(&q, 20);
enqueue(&q, 30);
enqueue(&q, 40);
enqueue(&q, 50);
enqueue(&q, 60);
Display(q);
printf("%d ", dequeue(&q));
return 0;
}