-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue implementation - dynamic circular array.cpp
94 lines (83 loc) · 1.89 KB
/
queue implementation - dynamic circular array.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
#include<bits/stdc++.h>
using namespace std;
//const int MAX_LENGTH = 100;
class arrayQueueType{
int Rear; //addition
int Front; //deletion
int Length; //count
//dynamic
int *arr;
int maxSize;
public:
//constructor
arrayQueueType(int Size){
if(Size <= 0)
maxSize = 100;
else
maxSize = Size;
Front = 0;
arr = new int[maxSize];
Rear = maxSize - 1;
Length = 0;
}
//functions
int isEmpty(){
return Length == 0;
}
bool isFull(){
return Length == maxSize;
}
void addQue(int Element){
if(isFull()){
cout << "Queue full!" << endl;
}
else{
Rear = (Rear + 1) % maxSize;
arr[Rear] = Element;
Length++;
}
}
void deleteQueue(){
if(isEmpty()){
cout << "empty queue!" << endl;
}
else{
Front = (Front + 1) % maxSize;
--Length;
}
}
int frontQueue(){ //get frony
//it takes boolean input and if true, it will continue the code, and if false it breaks and points to the like of the false statement
assert(!isEmpty());
return arr[Front];
} //or
/*void getFront(int &getFront){
getFront = arr[Front];
}*/
int RearQueue()
{
assert(!isEmpty());
return arr[Rear];
}
void printQueue()
{
assert(!isEmpty());
for(size_t i = Front; i != Rear + 1; i = (i + 1) % maxSize){
cout << arr[i] << " ";
}
//cout << arr[Rear];
}
};
int main(){
arrayQueueType q1(5);
q1.addQue(10);
q1.addQue(20);
q1.addQue(30);
q1.addQue(40);
q1.addQue(50);
//q1.addQue(60); // It will be added as it is dynamic array
//q1.deleteQueue();
//cout << q1.RearQueue();
q1.printQueue();
return 0;
}