forked from ashvish183/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_queue1.cpp
94 lines (79 loc) · 2.16 KB
/
circular_queue1.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
#include<bits/stdc++.h>
#define SIZE 10
using namespace std;
template < typename T > class Queue {
private:
T * arr;
int fIndex, rIndex;
public:
Queue() {
arr = new T[SIZE];
fIndex = 0;
rIndex = 0;
}
bool isFull() {
return (rIndex + 1) % SIZE == fIndex;
}
bool isEmpty() {
return rIndex == fIndex;
}
void enqueue(T item) {
if (isFull()) {
cout << "queue is full" << endl;
return;
}
*(this->arr + this->rIndex) = item;
this->rIndex = (this->rIndex + 1) % SIZE;
}
void dequeue() {
if (isEmpty()) {
cout << "queue is empty" << endl;
return;
}
cout<<front()<<" dequeued"<<endl;
fIndex = (fIndex + 1) % SIZE;
}
T front(){
return isEmpty() ? NULL : *(arr + fIndex);
}
};
int main(){
bool quit = false;
Queue < int > queue;
int temp;
do {
cout << "====================================" << endl;
cout << "select option :" << endl;
cout << "1 for enqueue" << endl;
cout << "2 for dequeue" << endl;
cout << "3 for front item" << endl;
cout << "4 for exit" << endl;
int ch;
cin >> ch;
cout << "====================================" << endl;
switch (ch) {
case 1:
cout << "enter item to enqueue:" << endl;
cin >> temp;
queue.enqueue(temp);
break;
case 2:
queue.dequeue();
break;
case 3:
if (queue.isEmpty()) {
cout << "queue is empty" << endl;
} else {
cout << "front: " << queue.front() << endl;
}
break;
case 4:
quit = true;
break;
default:
cout << "invalid selection" << endl;
break;
}
} while (!quit);
return 0;
}