-
Notifications
You must be signed in to change notification settings - Fork 44
/
circular_queue_using_array.cpp
125 lines (114 loc) · 1.8 KB
/
circular_queue_using_array.cpp
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
124
125
#include<stdio.h>
#define MAXQ 100
typedef struct
{
int front ,rear;
int items[MAXQ];
}queue;
int isempty(queue * q)
{
if(q->front==-1)
return 1;
else
return 0;
}
int isfull(queue *q)
{
if(q->front ==(q->rear+1)%MAXQ)
return 1;
else
return 0;
}
int insert(queue *q,int x)
{
if (isfull(q))
{
printf("\nqueue is full");
return 0 ;
}
if(q->front==-1)
{
q->front=0;
q->rear=0;
}
else
q->rear =(q->rear+1)%MAXQ;
q->items[q->rear] = x;
}
int Delete(queue *q)
{
int x;
if(isempty(q))
{
printf("\nqueue empty");
return 0;
}
x=q->items[q->front];
if(q->front == q->rear)
{
q->front =-1;
q->rear=-1;
}
else
q->front=(q->front +1)%MAXQ;
return x;
}
void display(queue *q)
{
int i;
if (isempty(q))
{
printf("\nqueue empty");
return;
}
else
printf("\nElements In the Queue Are:\n");
if(q->rear>=q->front)
{
for(i=q->front;i<=q->rear;i++)
printf("%d\n",q->items[i]);
}
else
for(i=q->front;i<=MAXQ;i++)
printf("%d\n",q->items[i]);
for(i=0;i<=q->rear;i++)
printf("%d\n",q->items[i]);
}
int main()
{
queue q;
int x;
char ch='1';
q.front=-1;
q.rear=-1;
while(ch!='4')
{
printf("\n 1 - Insert");
printf("\n 2 - Delete");
printf("\n 3 - Display");
printf("\n 4 - Quit");
printf("\nEnter your Choice :");
fflush(stdin);
scanf("%c",&ch);
switch(ch)
{
case '1':
printf("\nEnter The Element To Be inserted :");
scanf("%d",&x);
insert(&q,x);
break;
case '2':
x=Delete(&q);
printf("\nDeleted Element Is %d\n:",x);
break;
case '3':
display(&q);
break ;
case '4':
break;
default:
printf("\nYou Entered Wrong Choice.Try again!");
}
}
return 0;
}