-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularQueue.cpp
More file actions
174 lines (159 loc) · 3.64 KB
/
Copy pathCircularQueue.cpp
File metadata and controls
174 lines (159 loc) · 3.64 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <iostream>
using namespace std;
int const MaxSize = 5;
int circularQueue[MaxSize];
int rear = -1;
int front = -1;
bool empty() {
if (rear == -1)
return true;
else return false;
}
bool full() {
if ((front == 0 && rear == MaxSize - 1) || (front == rear + 1))
return true;
else
return false;
}
void enqueue(int el) {
if (full()) {
cout << "the queue is full." << endl;
exit(0);
}
else {
if (rear == -1) {
rear = 0;
front = 0;
}
else if (rear == MaxSize - 1)
rear = 0;
else
rear++;
circularQueue[rear] = el;
}
}
int dequeue()
{
int temp = 0;
if (empty()) {
cout << "the Queue is empty." << endl;
exit(0);
}
else
{
temp = circularQueue[front];
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == MaxSize - 1) {
front = 0;
}
else {
front++;
}
return temp;
}
}
int numberOfElement()
{
if (empty()) {
return 0;
}
else if (full()) {
return MaxSize;
}
else {
if (front > rear) {
return(MaxSize - front + rear + 1);
}
else {
return (rear - front + 1);
}
}
}
int numberOfspace() {
return MaxSize - numberOfElement();
}
int index(int i) {
if (empty()) {
cout << "the queue is empty\n";
exit(0);
}
return circularQueue[i];
}
int main()
{
int numOp, Op, enQnum, enQ, deQnum, deq;
cout << "enter the number of opratins: ";
cin >> numOp;
cout << "Oprations" << endl;
cout << "1: Enqueue" << endl;
cout << "2: Dequeue" << endl;
cout << "3: Full" << endl;
cout << "4: Empty" << endl;
cout << "5: number of Elelent" << endl;
cout << "6: number of Space" << endl;
cout << "7: index" << endl;
cout << "8: first" << endl;
cout << "9: last" << endl;
for (int i = 0; i < numOp; i++) {
cout << "select the opration: " << endl;
cin >> Op;
switch (Op)
{
case 1: {
cout << "how many element you want to enqueue: ";
cin >> enQnum;
for (int i = 0; i < enQnum; i++) {
cin >> enQ;
enqueue(enQ);
}
break;
}
case 2: {
cout << "how many element you want to dequeue: ";
cin >> deQnum;
for (int i = 0; i < deQnum; i++) {
cout << dequeue() << " ";
}
cout << "dequeued" << endl;
break;
}
case 3: {
if (full()) {
cout << "the Queue is full" << endl;
break;
}}
case 4: {
if (empty()) {
cout << "the Queue is empty" << endl;
break;
}
}
case 5: {
cout << "number of element is:" << numberOfElement() << endl;
break;
}
case 6: {
cout << "the number of space is: "<< numberOfspace() <<endl;
break;}
case 7: {
int numEn;
cout << "enter the index: ";
cin >> numEn;
cout << "\n enter the index: " << index(numEn);
break;
}
case 8: {
cout << "the first element" << firstElement()<<endl;
break;
}
case 9: {
cout << "the last element" << lastElement()<<endl;
break;
}
}
}
}