-
Notifications
You must be signed in to change notification settings - Fork 0
/
circularqueue.c
55 lines (52 loc) · 851 Bytes
/
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
#include<stdlib.h>
#include<stdio.h>
struct queue{
int front;
int rear;
int size;
int *Q;
};
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");
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");
}
void main()
{
struct queue q;
scanf("%d",&q.size);
q.Q=(int*)malloc(q.size*sizeof(int));
q.front=q.rear=-1;
enqueue(&q,1);
enqueue(&q,2);
enqueue(&q,3);
enqueue(&q,4);
enqueue(&q,5);
dequeue(&q);
dequeue(&q);
enqueue(&q,6);
display(q);
}