-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_queue.c
54 lines (53 loc) · 970 Bytes
/
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
#include<stdio.h>
#include<stdlib.h>
typedef struct queue
{
int size;
int front,rear;
int *a;
}queue;
void enqueue(queue *q,int x)
{
if((q->rear+1)%q->size==q->front)
printf("queue full\n");
else
{
q->rear=(q->rear+1)%q->size;
q->a[q->rear]=x;
}
}
int dequeue(queue *q)
{
int x=-1;
if(q->rear==q->front)//queue empty
return x;
else
{
q->front=(q->front+1)%q->size;
x=q->a[q->front];
return x;
}
}
void display(queue q)
{
int i=q.front+1;
do
{
printf("%d ",q.a[i]);
i=(i+1)%q.size;
}while(i!=(q.rear+1)%q.size);
printf("\n");
}
void main()
{
queue q;
printf("Enter the size\n");
scanf("%d",&q.size);
q.front=q.rear=0;
q.a=(int*)malloc(sizeof(int)*q.size);
enqueue(&q,4);
enqueue(&q,7);
enqueue(&q,9);
printf("%d\n",dequeue(&q));
display(q);
}