-
Notifications
You must be signed in to change notification settings - Fork 82
/
CircularQueue.cpp
84 lines (76 loc) · 2.18 KB
/
CircularQueue.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
#include <iostream>
using namespace std;
const int max_size = 100;
int insertq(int queue[max_size], int *front, int *rear, int *data) {
if ((*rear + 1) % max_size == *front) {
return -1;
} else {
if (*front == -1)
*front = 0;
*rear = (*rear + 1) % max_size;
queue[*rear] = *data;
return 1;
}
}
int delq(int queue[max_size], int *front, int *rear, int *data) {
if (*front == -1) {
return -1;
} else {
*data = queue[*front];
if (*front == *rear) {
*front = *rear = -1;
} else {
*front = (*front + 1) % max_size;
}
return 1;
}
}
void display(int queue[max_size], int front, int rear) {
if (front == -1) {
cout << "Queue is Empty" << endl;
return;
}
cout << "Queue elements: ";
int i = front;
do {
cout << queue[i] << " ";
i = (i + 1) % max_size;
} while (i != (rear + 1) % max_size);
cout << endl;
}
int main() {
int queue[max_size], data;
int front = -1, rear = -1, reply, option;
while (true) {
cout << "Enter 1 to insert an element\nEnter 2 to delete an element\nEnter 3 to display the elements\nEnter 4 to exit" << endl;
cin >> option;
switch (option) {
case 1:
cout << "Enter Number: ";
cin >> data;
reply = insertq(queue, &front, &rear, &data);
if (reply == -1) {
cout << "Queue is Full" << endl;
} else {
cout << data << " is inserted in the Queue" << endl;
}
break;
case 2:
reply = delq(queue, &front, &rear, &data);
if (reply == -1) {
cout << "Queue is Empty" << endl;
} else {
cout << "Deleted Number is: " << data << endl;
}
break;
case 3:
display(queue, front, rear);
break;
case 4:
exit(0);
default:
cout << "Enter a valid choice" << endl;
}
}
return 0;
}