-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcQ.cpp
87 lines (82 loc) · 1.17 KB
/
cQ.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
#include <iostream>
#define MAX 5
using namespace std;
class cQ
{
int arr[MAX];
int f , r;
public:
cQ()
{
f = r = 0;
}
int enQ(int);
int deQ();
void display();
};
int cQ::enQ(int ele)
{
if (r+1 == f || (r+1)%MAX == f)
{cout << "\nQueue full!";return -1;}
else if(f == 0 && r == MAX -1)
{cout << "\nQueue full!";return -1;}
else if (r == MAX - 1 && f !=0)
{
r = 0;
arr[r] = ele;
return 0;
}
else
arr[++r] = ele;
return 0;
}
int cQ::deQ()
{
if (f == r)
{
cout << "\nQueue empty!";
return -1;
}
f++;
return arr[f];
}
void cQ::display()
{
if (f == r)
{
cout << "\nQueue empty!";
return;
}
for (int i = (f+1)%MAX; i != r; i = (i+1)%MAX)
cout << arr[i] << " ";
cout << arr[r];
}
int main()
{
int ch,num;
cQ q1;
do
{
cout << "\n1.EnQ\n2.DeQ\n3.Display\n4.Exit";
cout << "\nEnter your choice: ";
cin >> ch;
switch(ch)
{
case 1:
cout << "\nEnter element to be inserted: ";
cin >> num;
q1.enQ(num);
break;
case 2:
cout << "\nThe deleted element is: " << q1.deQ();
break;
case 3:
cout << "\nThe queue is as follows: ";
q1.display();
break;
default:
break;
}
}while(ch != 4);
return 0;
}