Skip to content

Commit 08137ed

Browse files
authored
Merge pull request #146 from kartikeysingh6/master
Added Circular Queue in C++ DS
2 parents 8d39af3 + 7c3cb5c commit 08137ed

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include<iostream>
2+
using namespace std;
3+
int f=-1;
4+
int r=-1;
5+
int N=3;
6+
int Q[3];
7+
8+
void Enq(){int x;
9+
if((f==0 && r==N-1) || f-r==1){
10+
cout<<"Queue if Full!"<<endl;
11+
return;
12+
}
13+
cout<<"Enter element to insert: ";
14+
cin>>x;
15+
r=(r+1)%N;
16+
Q[r]=x;
17+
if(f==-1)
18+
f=0;
19+
}
20+
21+
void Deq(){
22+
if(f==-1){
23+
cout<<"Queue is Empty!"<<endl;
24+
return;
25+
}
26+
if(f==r){
27+
cout<<"Removed element is "<<Q[f];
28+
f=r=-1;
29+
return;
30+
}
31+
cout<<"Removed element is "<<Q[f];
32+
f=(f+1)%N;
33+
}
34+
35+
void Peek(){
36+
if(f==-1){
37+
cout<<"Queue is Empty!"<<endl;
38+
return;
39+
}
40+
cout<<"Front: "<<Q[f];
41+
cout<<" Rear: "<<Q[r]<<endl;
42+
}
43+
44+
int main(){int n=0;
45+
while(n!=4){
46+
cout<<endl<<"1.Enqueue"<<endl<<"2.Dequeue"<<endl<<"3.Peek"<<endl<<"4.Exit"<<endl;
47+
cout<<"Enter choice: ";
48+
cin>>n;
49+
cout<<endl;
50+
switch(n){
51+
case 1: Enq();
52+
break;
53+
case 2: Deq();
54+
break;
55+
case 3: Peek();
56+
break;
57+
case 4: return 0;
58+
default: break;
59+
}
60+
}
61+
return 0;
62+
}

0 commit comments

Comments
 (0)