-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathcircular_queue.cpp
More file actions
62 lines (58 loc) · 916 Bytes
/
Copy pathcircular_queue.cpp
File metadata and controls
62 lines (58 loc) · 916 Bytes
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
#include<iostream>
using namespace std;
int f=-1;
int r=-1;
int N=3;
int Q[3];
void Enq(){int x;
if((f==0 && r==N-1) || f-r==1){
cout<<"Queue if Full!"<<endl;
return;
}
cout<<"Enter element to insert: ";
cin>>x;
r=(r+1)%N;
Q[r]=x;
if(f==-1)
f=0;
}
void Deq(){
if(f==-1){
cout<<"Queue is Empty!"<<endl;
return;
}
if(f==r){
cout<<"Removed element is "<<Q[f];
f=r=-1;
return;
}
cout<<"Removed element is "<<Q[f];
f=(f+1)%N;
}
void Peek(){
if(f==-1){
cout<<"Queue is Empty!"<<endl;
return;
}
cout<<"Front: "<<Q[f];
cout<<" Rear: "<<Q[r]<<endl;
}
int main(){int n=0;
while(n!=4){
cout<<endl<<"1.Enqueue"<<endl<<"2.Dequeue"<<endl<<"3.Peek"<<endl<<"4.Exit"<<endl;
cout<<"Enter choice: ";
cin>>n;
cout<<endl;
switch(n){
case 1: Enq();
break;
case 2: Deq();
break;
case 3: Peek();
break;
case 4: return 0;
default: break;
}
}
return 0;
}