-
Notifications
You must be signed in to change notification settings - Fork 163
/
cq.cpp
90 lines (81 loc) · 1.69 KB
/
cq.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
#include <iostream>
using namespace std;
const int SIZE=3;
class CQueue{
private:
int front;
int rear;
int arr[SIZE];
public:
CQueue(){
front=-1;
rear=-1;
}
bool isEmpty(){
return (front==-1 && rear==-1);
}
bool isFull(){
return (rear+1)%SIZE==front;
}
void enq(int elem){
if(isFull()){
cout<<"Queue Full"<<endl;
return;
}
if(isEmpty())
front++;
rear=(rear+1)%SIZE;
arr[rear]=elem;
}
int deq(){
if(isEmpty()){
cout<<"Empty queue"<<endl;
return -1;
}
int elem=arr[front];
if (front==rear)
front=rear=-1;
else
front=(front+1)%SIZE;
return elem;
}
void display(){
int i;
for (i=front;i!=rear;i=(i+1)%SIZE){
cout<<arr[i]<<' ';
}
cout<<arr[i]<<endl;
}
};
int main(){
CQueue cq;
int choice, item;
do
{
cout<<"1.Insert"<<endl;
cout<<"2.Delete"<<endl;
cout<<"3.Display"<<endl;
cout<<"4.Quit"<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Input the element for insertion in queue : ";
cin>>item;
cq.enq(item);
break;
case 2:
cq.deq();
break;
case 3:
cq.display();
break;
case 4:
break;
default:
cout<<"Wrong choice"<<endl;
}
} while(choice != 4);
return 0;
}