-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcircular_equeue.c
More file actions
87 lines (86 loc) · 2.18 KB
/
Copy pathcircular_equeue.c
File metadata and controls
87 lines (86 loc) · 2.18 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
#include<stdio.h>
#include<stdlib.h>
#define max 5
int isfull(int);
int isempty(int);
void cenqueue(int,int *,int *,int []);
int cdequeue(int *,int *,int []);
void display(int,int,int []);
int main()
{
int cq[max];
int front=0,rear=-1,ch,count=0;
while(1)
{
printf("1.enqueue\n2.dequeue\n3.display\n4.exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(isfull(count))
printf("circular queue is full\n");
else
{
int ele;
printf("Enter the element to be inserted in the circular queue : ");
scanf("%d",&ele);
cenqueue(ele,&rear,&count,cq);
}
break;
case 2: if(isempty(count))
printf("circular queue is empty\n");
else
{
int ele;
ele=cdequeue(&front,&count,cq);
printf("The deleted element from the circular queue is %d\n",ele);
}
break;
case 3: display(front,count,cq);
break;
case 4: exit(0);
default:printf("Invalid choice! please try again\n") ;
break;
}
}
return 0;
}
int isfull(int a)
{
if(a==max)
return 1;
return 0;
}
int isempty(int a)
{
if(a==0)
return 1;
return 0;
}
void cenqueue(int e,int *r,int *c,int q[])
{
(*r)=((*r)+1)%max;
q[*r]=e;
(*c)++;
}
int cdequeue(int *r,int *c,int q[])
{
int val=q[(*r)];
(*r)=((*r+1))%max;
(*c)--;
return val;
}
void display(int front, int count, int q[])
{
if (isempty(count))
{
printf("Circular queue is empty\n");
return;
}
printf("Elements in the circular queue: ");
for (int i = 0; i < count; i++) {
// Calculate the actual index in the queue array
int index = (front + i) % max;
printf("%d ", q[index]);
}
printf("\n");
}