-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.cpp
More file actions
60 lines (52 loc) · 1.02 KB
/
Copy pathQueue.cpp
File metadata and controls
60 lines (52 loc) · 1.02 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
#include <bits/stdc++.h>
#define ll long long
#define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << "\n";
#define ln cout<<endl;
using namespace std;
const int MAX_SIZE = 100;
class Queue {
private:
int items[MAX_SIZE];
int front;
int rear;
int count;
public:
Queue() {
front = 0;
rear = MAX_SIZE - 1;
count = 0;
}
int length() {
return count;
}
void enqueue(int element) {
rear = (rear + 1) % MAX_SIZE;
items[rear] = element;
count++;
}
int dequeue() {
int element = items[front];
front = (front + 1) % MAX_SIZE;
count--;
return element;
}
int search_queue(int item) {
for (int i = 0; i < count; i++) {
int index = (front + i) % MAX_SIZE;
if (items[index] == item) {
return i;
}
}
return -1;
}
};
int main() {
Queue q;
q.enqueue(1);
q.enqueue(2);
q.enqueue(3);
cout << q.length() << endl; // 3
cout << q.dequeue() << endl; // 1
cout << q.search_queue(2) << endl; // 0
return 0;
}