-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircular_Queue_operations.cpp
More file actions
81 lines (77 loc) · 1.17 KB
/
Copy pathCircular_Queue_operations.cpp
File metadata and controls
81 lines (77 loc) · 1.17 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
68
69
70
71
72
73
74
75
76
77
78
79
80
#include<iostream>
using namespace std;
#define SIZE 5
class CircularQueue{
int arr[SIZE];
int front, rear, count;
public:
CircularQueue(){
front = 0;
rear = -1;
count = 0;
}
bool isfull(){
return (count == SIZE);
}
bool isempty(){
return (count == 0);
}
void enqueue(int x){
if(isfull()){
cout<<"full!"<<endl;
return;
}
rear = (rear + 1) % SIZE;
arr[rear]=x;
count++;
}
void dequeue(){
if(isempty()){
cout<<"Empty!"<<endl;
return;
}
cout<<arr[front]<<" removed."<<endl;
front = (front + 1) % SIZE;
count--;
}
int peek(){
if(isempty()){
cout<<"Empty!"<<endl;
return -1;
}
return arr[front];
}
int getRear(){
if(isempty()){
cout<<"Empty."<<endl;
return -1;
}
return arr[rear];
}
void display(){
if(isempty()){
cout<<"Empty!"<<endl;
return;
} else {
for(int i=0; i<count; i++){
cout<<arr[(front + i) % SIZE] <<" ";
}
cout<<endl;
}
}
};
int main(){
CircularQueue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.display();
q.dequeue();
q.enqueue(40);
q.enqueue(50);
q.display();
q.enqueue(60);
q.display();
cout<<"Front= "<<q.peek()<<", Rear="<<q.getRear()<<endl;
return 0;
}