-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcircle-queue.cpp
58 lines (54 loc) · 2.07 KB
/
circle-queue.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
#include <iostream>
#include <vector>
// CircularQueue class definition
class CircularQueue {
private:
std::vector<int> queue; // Vector to store queue elements
int maxSize; // Maximum size of the queue
int head; // Index of the front element
int tail; // Index of the rear element
public:
// Constructor to initialize the queue with a given size
CircularQueue(int size) : maxSize(size), head(-1), tail(-1) {
queue.resize(size); // Resize the vector to the specified size
}
// Function to add an element to the queue
void enqueue(int data) {
// Check if the queue is full
if ((tail + 1) % maxSize == head) {
std::cout << "Circular Queue is full\n";
}
// Check if the queue is empty
else if (head == -1) {
head = 0; // Set head to the first position
tail = 0; // Set tail to the first position
queue[tail] = data; // Add the data to the queue
}
// Add the data to the queue when it is not full or empty
else {
tail = (tail + 1) % maxSize; // Move tail to the next position
queue[tail] = data; // Add the data to the queue
}
}
// Function to remove an element from the queue
int dequeue() {
// Check if the queue is empty
if (head == -1) {
std::cout << "Circular Queue is empty\n";
return -1; // Return -1 to indicate the queue is empty
}
// Check if there is only one element in the queue
else if (head == tail) {
int temp = queue[head]; // Store the data to return
head = -1; // Reset head to indicate the queue is empty
tail = -1; // Reset tail to indicate the queue is empty
return temp; // Return the data
}
// Remove the data from the queue when it is not empty
else {
int temp = queue[head]; // Store the data to return
head = (head + 1) % maxSize; // Move head to the next position
return temp; // Return the data
}
}
};