-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_queue.cpp
executable file
·103 lines (99 loc) · 1.78 KB
/
circular_queue.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
#include <iostream>
using namespace std;
class CircularQueue
{
private:
int front;
int rear;
int arr[5];
public:
CircularQueue()
{
front = -1;
rear = -1;
for (int i = 0; i < 5; i++)
{
arr[i] = 0;
}
}
bool isEmpty()
{
if (front == -1 && rear == -1)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if ((rear + 1) % 5 == front)
{
return true;
}
else
{
return false;
}
}
void enqueued(int a)
{
if (isFull())
{
cout << "Queue FUll" << endl;
}
else if (isEmpty())
{
rear = front = 0;
arr[rear] = a;
}
else
{
rear = (rear + 1) % 4;
arr[rear] = a;
}
}
int dequeued()
{
int x;
if (isEmpty())
{
cout << "Empty Queue Nothinng to Remove" << endl;
}
else if (front == rear)
{
x = arr[front];
arr[front] = 0;
rear = front = -1;
}
else
{
x = arr[front];
arr[front] = 0;
front++;
}
return x;
}
void display()
{
for (int i = 0; i < 5; i++)
{
cout << arr[i] << " ";
}
}
};
int main()
{
CircularQueue one;
one.enqueued(5);
one.enqueued(10);
one.enqueued(25);
one.enqueued(30);
one.enqueued(50);
one.dequeued();
one.enqueued(100);
one.display();
return 0;
}