-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcircular_queue.c
More file actions
110 lines (103 loc) · 1.61 KB
/
Copy pathcircular_queue.c
File metadata and controls
110 lines (103 loc) · 1.61 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#define SIZE 10
void insert(int cq[],int *f,int *r,int n)
{
if((*f)==(*r+1)%SIZE)
{
printf("\n Circular queue overflow");
return;
}
if((*r)==-1)
{
(*f)=(*r)=0;
}
else if((*f)!=0&&(*r)==SIZE-1)
{
(*r)=0;
}
else
{
(*r)++;
}
cq[*r]=n;
}
int delete(int cq[],int *f,int *r)
{
int n;
if((*f)==-1)
{
printf("Circular queue underflow");
return;
}
n=cq[*f];
if(*f==*r)
{
*f=*r=-1;
}
else if(*f==SIZE-1)
{
*f=0;
}
else
{
(*f)++;
}
return n;
}
void traverse(int cq[],int f,int r)
{
if(f==-1)
{
printf("Circular Queue underflow");
return;
}
if(f<=r)
{
for(int i=f;i<=r;i++)
{
printf("%d",cq[i]);
}
}
else
{
for(int i=f;i<=SIZE-1;i++)
{
printf("%d",cq[i]);
}
for(int i=0;i<=r;i++)
{
printf("%d",cq[i]);
}
}
}
int main()
{
int cq[SIZE];
int f=-1,r=-1;
int n,ch;
do{
printf("\n*****MENU*****");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. traverse");
printf("\n4. Exit");
printf("\n***************");
printf("\nEnter Your Choice 1/2/3/4 : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("\nEnter value to insert : ");
scanf("%d",&n);
insert(cq,&f,&r,n);
break;
case 2: n=delete(cq,&f,&r);
printf("\n%d deleted",n);
break;
case 3: traverse(cq,f,r);
break;
case 4: printf("\n\n Thank you");
break;
default:printf("\nWrong Choice....");
}
}while(ch!=4);
}