-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue as an array.cpp
More file actions
74 lines (67 loc) · 890 Bytes
/
queue as an array.cpp
File metadata and controls
74 lines (67 loc) · 890 Bytes
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
#include<iostream>
#define max 10
using namespace std;
int q[max],f=-1,r=-1;
bool isempty()
{
return(f==-1 && r==-1);
}
bool isfull()
{
return (r+1)%max==f?true:false;
}
void enqueue(int new_data)
{
if(isfull())
{
cout<<"queue is full\n";
return;
}
if(isempty())
{
f=r=0;
}
else
{
r=(r+1)%max;
}
q[r]=new_data;
}
void dequeue()
{
if(isempty())
{
cout<<"queue is empty\n";
return;
}
cout<<q[f]<<endl;
if(f==r)
{
f=r=-1;
}
else
{
f=(f+1)%max;
}
}
int main()
{
int t,x;
loop:
cout<<"enter 1 to engueue\n";
cout<<"enter 2 to dequeue\n";
cout<<"enter 3 to exit\n";
cin>>t;
switch(t)
{
case 1:cout<<"enter data to be enqueued\n";
cin>>x;
enqueue(x);
break;
case 2:dequeue();
break;
case 3:exit(0);
}
goto loop;
return 0;
}