-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcq_array.c
106 lines (106 loc) · 1.5 KB
/
cq_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
#include<stdio.h>
#define SIZE 6
int cq[SIZE];
void insertcq(int);
void deletecq();
void display();
void exit();
int front=-1,rear=-1;
int main()
{
int n,ch,i=1;
while(i)
{
printf("\n circular queue:\n0.Exit\n1.insert\n2.delete\n3.display");
printf("\nenter choice 0-3:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter number:");
scanf("%d",&n);
insertcq(n);
break;
case 2:
deletecq();
break;
case 3:
display();
break;
case 0:
i=0;
break;
default:
printf("\nSorry invalid option");
}
}
}
void insertcq(int item)
{
if((front==0&&rear==SIZE-1)||(front==rear+1))
{
printf("q is full..");
return ;
}
else if(rear==-1)
{
rear++;
front++;
}
else if(rear==SIZE-1&& front>0)
{
rear=0;
}
else
{
rear++;
}
cq[rear]=item;
}
void display()
{
int i;
printf("\nThe queue elements are:");
if(front>rear)
{
for(i=front;i<SIZE;i++)
{
printf("%3d ",cq[i]);
}
for(i=0;i<=rear;i++)
{
printf("%3d",cq[i]);
}
}
else
{
for(i=front;i<=rear;i++)
printf("%3d",cq[i]);
}
}
void deletecq()
{
if(front==-1&&rear==-1)
{
printf("queue is empty");
}
else if(front==rear)
{
printf("\n %d deleted",cq[front]);
front=-1;
rear=-1;
}
else
{
if(front==SIZE-1)
{
printf("\n %d deleted",cq[front]);
front=0;
}
else
{
printf("\n %d is deleting element",cq[front]);
front++;
}
}
}