-
Notifications
You must be signed in to change notification settings - Fork 1
/
Circular_queue.c
69 lines (69 loc) · 1.35 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
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
struct queue
{
int size;
int front;
int rear;
int *A;
};
void create(struct queue *q)
{
printf("Enter size of queue:\n");
scanf("%d", &(*(q)).size);
(*(q)).front = (*(q)).rear = 0;
(*(q)).A = (int *)malloc((*(q)).size * sizeof(int));
}
void enqueue(struct queue *q, int num)
{
if ((*(q)).front == ((*(q)).rear + 1) % (*(q)).size)
{
printf("Queue is full!\n");
}
else
{
(*(q)).rear = ((*(q)).rear + 1) % (*(q)).size;
(*(q)).A[(*(q)).rear] = num;
}
}
int dequeue(struct queue *q)
{
int x = (-1);
if ((*(q)).front == (*(q)).rear)
{
return x;
}
else
{
(*(q)).front = ((*(q)).front + 1) % (*(q)).size;
x = (*(q)).A[(*(q)).front];
return x;
}
}
void display(struct queue *q)
{
if ((*(q)).front == (*(q)).rear)
{
printf("Queue is empty!\n");
}
else
{
int a = (*(q)).front + 1;
while (a != ((*(q)).rear + 1) % (*(q)).size)
{
printf("%d ", (*(q)).A[a]);
a = (a + 1) % (*(q)).size;
}
}
}
int main()
{
struct queue q;
create(&q);
enqueue(&q, 1);
enqueue(&q, 2);
enqueue(&q, 3);
enqueue(&q, 4);
enqueue(&q, 5);
display(&q);
}