forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularQueue.cpp
91 lines (67 loc) · 1.24 KB
/
CircularQueue.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
//To implement a static circular queue
#include <iostream>
using namespace std;
#define MAXSIZE 5
class queue //making an object queue
{
int arr[MAXSIZE];
int front = -1; //initialising front
int rear = -1; //initialising rear
public:
void add (int x) //adding an element to the queue
{
if (rear == -1)
{
front = 0;
rear = 0;
}
else if (rear + 1 == front || (rear == MAXSIZE - 1 && front == 0))
cout << endl << "Overflow" << endl;
else if (rear == MAXSIZE - 1)
rear = 0;
else
rear++;
arr[rear] = x;
}
int rem () //removing an element to the queue
{
int x = 0;
if (front == -1)
cout << endl << "Underflow" << endl;
else if (front == rear)
{
x = arr[front];
front = rear = -1;
}
else if (front == MAXSIZE - 1)
{
x = arr[front];
front = 0;
}
else
x = arr[front++];
return x;
}
};
queue q;
int main ()
{
int x;
do
{
cout << "1.Add\n2.Remove\n3.Exit\n";
cin >> x;
if (x == 1)
{
int n;
cin >> n;
q.add (n);
}
if (x == 2)
{
cout << endl << q.rem () << endl;
}
}
while (x != 3);
return 0;
}