-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcircularqueue.c
More file actions
64 lines (56 loc) · 1.15 KB
/
Copy pathcircularqueue.c
File metadata and controls
64 lines (56 loc) · 1.15 KB
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
#include<stdio.h>
#include<stdlib.h>
struct queue{
int size;
int*arr;
int r;
int f;
}*q,*n;
int isempty(struct queue *q){
if(q->r==q->f){
printf("Empty \n");
return 1;}
return 0;
}
int isfull(struct queue *q){
if((q->r+1)% q->size==q->f){ //1=2,2=3
printf("Full\n");
return 1;}
return 0;
}
void enqueue(struct queue* q){
if(isfull(q))
return ;
q->r=(q->r+1)%q->size;
scanf("%d",&q->arr[q->r]);
}
int dequeue(struct queue* q){
if(isempty(q))
return 0;
q->f=(q->f+1)%q->size;
int x=q->arr[q->f];
return x;
}
int main(){
q=(struct queue*)malloc(sizeof(struct queue));
printf("Enter size :\n");
scanf("%d",&q->size);
q->r=q->f=0; //for full q=f
q->arr==(int*)malloc(sizeof(int)*q->size);
printf("Enter elements :\n");
for(int i=0;i<q->size;i++)
enqueue(q);
printf("Elements are :\n");
while(q->f!=q->r){
q->f=(q->f+1)%q->size;
printf("enqueue :%d\n",q->arr[q->f]);
}
for(int i=0;i<q->size;i++)
printf("dequeue :%d\n",dequeue(q));
for(int i=0;i<q->size;i++)
enqueue(q);
while(q->f!=q->r){
q->f=(q->f+1)%q->size;
printf(" new :%d\n",q->arr[q->f]);
}
}