-
Notifications
You must be signed in to change notification settings - Fork 2
/
Circular-Queue-Array.c
106 lines (97 loc) · 1.37 KB
/
Circular-Queue-Array.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
103
104
105
106
//Program-Circular Queue - Using Array
#include<stdio.h>
#define max 5
int cq[max],rear=-1,front=-1;
void enque(int n)
{
if(front==(rear+1)%max)
printf("Circular Queue is Full !....");
else if(front==-1 && rear==-1)
{
front=0;
rear=0;
cq[rear]=n;
}
else
{
rear=(rear+1)%max;
cq[rear]=n;
}
}
void deque()
{
if(front==-1)
printf("\nCircular Queue is Empty !....");
else
{
printf("\nThe deleted Element is %d ",cq[front]);
if(front==rear)
{
front=-1;
rear=-1;
}
else
front=(front+1)%max;
}
}
void display()
{
int f;
if(front==-1)
printf("\nCircular Queue is Empty !....");
else
{
printf("\nQueue contents : ");
if(front<=rear)
{
f=front;
while(f<=rear)
{
printf("%3d",cq[f]);
f++;
}
}
else if(rear<front)
{
f=front;
while(f<max)
{
printf("%3d",cq[f]);
f++;
}
f=0;
while(f<=rear)
{
printf("%3d",cq[f]);
f++;
}
}
}
}
void main()
{
int ch,n;
while(1)
{
printf("\n1.Insertion\n2.Deletion\n3.Display\n4.Exit\nEnter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: if(front==(rear+1)%max)
printf("Circular Queue is Full !....");
else
{
printf("\nEnter the number to be inserted : ");
scanf("%d",&n);
enque(n);
}
break;
case 2: deque();
break;
case 3: display();
break;
case 4:
break;
}
}
}