-
Notifications
You must be signed in to change notification settings - Fork 0
/
circular_queues.cpp
82 lines (65 loc) · 1.82 KB
/
circular_queues.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
#include <iostream>
class CircularQueue {
private:
int* arr;
int capacity;
int front;
int rear;
int size;
public:
CircularQueue(int capacity) : capacity(capacity), front(0), rear(-1), size(0) {
arr = new int[capacity];
}
~CircularQueue() {
delete[] arr;
}
// Function to enqueue an element into the circular queue
bool enqueue(int data) {
if (isFull()) {
std::cout << "Queue is full. Enqueue operation failed." << std::endl;
return false;
}
rear = (rear + 1) % capacity;
arr[rear] = data;
size++;
return true;
}
// Function to dequeue an element from the circular queue
bool dequeue() {
if (isEmpty()) {
std::cout << "Queue is empty. Dequeue operation failed." << std::endl;
return false;
}
front = (front + 1) % capacity;
size--;
return true;
}
// Function to get the front element of the circular queue
int peek() {
if (isEmpty()) {
std::cout << "Queue is empty. Peek operation failed." << std::endl;
return -1;
}
return arr[front];
}
// Function to check if the circular queue is empty
bool isEmpty() {
return size == 0;
}
// Function to check if the circular queue is full
bool isFull() {
return size == capacity;
}
};
int main() {
CircularQueue cq(5);
cq.enqueue(10);
cq.enqueue(20);
cq.enqueue(30);
cq.enqueue(40);
std::cout << "Front element: " << cq.peek() << std::endl;
cq.dequeue();
cq.enqueue(50);
std::cout << "Front element after dequeuing and enqueuing: " << cq.peek() << std::endl;
return 0;
}