-
Notifications
You must be signed in to change notification settings - Fork 72
/
circular-queue.c
102 lines (97 loc) · 1.23 KB
/
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
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
#include<stdio.h>
#include<stdlib.h>
int n=5,front=0,rear=0,q[10];
void enqueue()
{
if(( (front==0) && (rear==n-1) ) || (front==rear+1) )
{
printf("the queue is full\n");
}
else
{
int k;
printf("the element to be inserted\n");
scanf("%d",&k);
if(rear!=n-1)
{
q[rear++]=k;
}
else
{
q[rear]=k;
rear=0;
}
}
}
void dequeue()
{
if(front==rear)
{
printf("the queue is empty\n");
}
else
{
printf("the element removed is %d\n",q[front]);
if(front==n-1)
{
front=0;
}
else
{
front++;
}
}
}
void print()
{
int i;
if(rear>front)
{
printf("the elements in the queue are\n");
for(i=front;i<rear;i++)
{
printf("%d ",q[i]);
}
printf("\n");
}
if(rear<front)
{
printf("the elements in the queue are\n");
for(i=front;i<n;i++)
{
printf("%d ",q[i]);
}
for(i=0;i<rear;i++)
{
printf("%d ",q[i]);
}
printf("\n");
}
if(front==rear)
{
printf("the queue is empty\n");
}
}
int main()
{
while(1)
{
int n;
printf("select any one option\n1.enque\n2.deque\n3.print\n4.exit\n");
scanf("%d",&n);
switch(n)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
print();
break;
case 4:
exit(0);
}
}
}