-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircular_Queue.cpp
135 lines (116 loc) · 3.12 KB
/
Circular_Queue.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
using namespace std;
/*---------------------------------------------------------------------------*/
class QUEUE{
private:
int cqueue[5];
int front, rear, n;
public:
QUEUE(); //default constructor
void insertCQ(int);
void deleteCQ();
void displayCQ();
};
/*---------------------------------------------------------------------------*/
QUEUE :: QUEUE(){
front = -1;
rear = -1;
n = 5;
}
/*---------------------------------------------------------------------------*/
void QUEUE :: insertCQ(int val){
if (front == (rear + 1) % n){
cout << "Queue Overflow\n";
return;
}
if (front == -1){
front = 0;
rear = 0;
}
else{
rear = (rear + 1) % n;
}
cqueue[rear] = val;
}
/*---------------------------------------------------------------------------*/
void QUEUE::deleteCQ(){
if(front == -1){
cout << "Queue Underflow\n";
return;
}
cout << "Element deleted from queue is " << cqueue[front] << endl;
if (front == rear){
front = -1;
rear = -1;
}
else{
front = (front + 1) % n;
}
}
/*---------------------------------------------------------------------------*/
void QUEUE :: displayCQ(){
int f = front;
int r = rear;
if(f <= r){
while(f <= r){
cout << cqueue[f] << " ";
f++;
}
}
else{
while(f <= (n - 1)){
cout << cqueue[f] << " ";
f++;
}
f = 0;
while(f <= r){
cout << cqueue[f] << " ";
f++;
}
}
cout << endl;
}
/*---------------------------------------------------------------------------*/
// Objects
QUEUE q1;
QUEUE q2;
/*---------------------------------------------------------------------------*/
int main(){
int ch, val;
cout << "1 —————> Insert in Queue 1\n";
cout << "2 —————> Insert in Queue 2\n";
cout << "3 —————> Delete from Queue 1\n";
cout << "4 —————> Delete from Queue 2\n";
cout << "5 —————> Display Queue 1\n";
cout << "6 —————> Display Queue 2\n";
cout << "7 —————> Exit\n";
do{
cout << "\nEnter your choice : ";
cin >> ch;
switch(ch){
case (1):
cout << "Enter the value to be inserted : ";
cin >> val;
q1.insertCQ(val);
break;
case (2):
cout << "Enter the value to be inserted : ";
cin >> val;
q2.insertCQ(val);
break;
case (3):
q1.deleteCQ();
break;
case (4):
q2.deleteCQ();
break;
case (5):
q1.displayCQ();
break;
case (6):
q2.displayCQ();
break;
}
}while(ch != 7);
}
/*---------------------------------------------------------------------------*/