-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathimplementationOfQueuesUsingArrays.cpp
53 lines (53 loc) · 1.09 KB
/
implementationOfQueuesUsingArrays.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
#include<iostream>
#include<vector>
using namespace std;
class Queue{
private:
int front;
int back;
vector<int> v;
public:
Queue(){
this->back = -1;
this->front = -1;
}
void enqueue(int data){
v.push_back(data);
this->back++;
if(this->back==0){
this->front = 0;
}
}
void dequeue(){
if(this->front==this->back){
this->front = -1;
this->back = -1;
this->v.clear();
}
else{
this->front++;
}
}
int getFront(){
if(this->front==-1){
return -1;
}
return this->v[this->front];
}
bool isEmpty(){
return this->front==-1;
}
};
int main(){
Queue q1;
q1.enqueue(10);
q1.enqueue(20);
q1.enqueue(30);
q1.dequeue();
q1.enqueue(40);
while(!q1.isEmpty()){
cout<<q1.getFront()<<" ";
q1.dequeue();
}
return 0;
}