-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCircular array based queue 1.cpp
More file actions
67 lines (64 loc) · 1.14 KB
/
Circular array based queue 1.cpp
File metadata and controls
67 lines (64 loc) · 1.14 KB
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
#include<iostream>
using namespace std;
class Queue
{
int *arr;
int ms; //max size
int cs; //current size
int front; // First element in a queue
int rear; // First element in a rear
public:
Queue(int default_size = 7)
{
this->front = 0; //front point to 0th index
this->rear = default_size -1;
this->ms = default_size;
this->arr = new int[this->ms]();
this->cs = 0;
}
bool isFull()
{
return this->cs == this->ms; //Complete space is used
}
bool isempty()
{
return this->cs == 0;
}
void enqueue(int data)
{
if(!isFull())
{
this->rear = (this->rear + 1) % this->ms;
this->arr[this->rear] = data;
this->cs++;
}
}
void dequeue()
{
if(!isempty())
{
this->front = (this->front + 1) % this->ms;
this->cs -=1;
}
}
int getfront()
{
return this->arr[this->front];
}
};
int main(int argc, char const *argv[])
{
Queue q(10);
for(int i = 1 ; i <= 6 ; i++)
{
q.enqueue(i);
}
q.dequeue();
q.enqueue(8);
while(!q.isempty())
{
cout<<q.getfront()<<endl;
q.dequeue();
}
return 0;
}