-
Notifications
You must be signed in to change notification settings - Fork 0
/
06_01_CQ_arr.c
123 lines (106 loc) · 1.69 KB
/
06_01_CQ_arr.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <stdio.h>
#include <stdlib.h>
#define max 5
static int count=0;
int cq[max],value,x;
int front = -1;
int rear = -1;
void enqueue();
void dequeue();
void print();
int main()
{
int choice;
system("cls");
system("color A");
do
{
line:
printf("\nPress 1 to add value to circular queue");
printf("\nPress 2 to del value from circular queue");
printf("\nPress 3 to print all the values of circular queue");
printf("\nPress 0 to exit");
printf("\n\nEnter choice (0-3) : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(count==max)
printf("\nCircular queue id full\n");
else
enqueue(value);
goto line;
case 2:
dequeue();
goto line;
case 3:
print();
goto line;
case 0:
exit(0);
break;
default:
printf("\n\nInvalid Choice\n\n");
goto line;
}
} while(choice!=0);
return 0;
}
void enqueue()
{
if(count==max)
{
printf("\ncircular queue is full\n");
}
else
{
printf("\nEnter the value you want to add : ");
scanf("%d",&value);
cq[rear] = value;
rear = (rear+1) % max;
}
count++;
}
void dequeue()
{
if(count == 0)
{
printf("\ncircular queue is empty\n");
}
else
{
x = cq[front];
front = (front +1) % max;
count--;
}
}
void print()
{
if(count == 0)
{
printf("\ncircular queue is empty\n");
}
else
{
if(rear>front)
{
int i;
for(i=front;i<rear;i++)
{
printf("%d\n",cq[i]);
}
}
else
{
int i;
for(i=front;i<max;i++)
{
printf("%d\n",cq[i]);
}
for(i=0;i<rear;i++)
{
printf("%d\n",cq[i]);
}
}
}
}