Dequeue Operation in Queues
Enqueue Operation in Queues
Introduction to Queues
Queues are a fundamental data structure in computer science, used to manage collections of elements in a linear, first-in, first-out (FIFO) order. However, standard queues can sometimes be inefficient in their use of memory. This is where circular queues come into play, optimizing space usage by treating the queue as a circular buffer.
A circular queue, also known as a ring buffer, is a type of queue in which the last position is connected back to the first position to make a circle. This connection allows the circular queue to efficiently utilize the available space by reusing the vacated spaces, thus overcoming the limitations of linear queues where space might be wasted if the front and rear pointers continue to move in one direction.
front
and rear
, to keep track of the positions for dequeuing and enqueuing operations.Let's look at how a circular queue can be implemented in C++.
#include <iostream>
#define MAX 5
class CircularQueue {
int arr[MAX];
int front, rear;
public:
CircularQueue() : front(-1), rear(-1) {}
bool isFull() {
return (front == 0 && rear == MAX - 1) || (front == rear + 1);
}
bool isEmpty() {
return front == -1;
}
void enqueue(int value) {
if (isFull()) {
std::cout << "Queue is Full" << std::endl;
return;
}
if (front == -1) {
front = 0;
}
rear = (rear + 1) % MAX;
arr[rear] = value;
std::cout << value << " added to the queue" << std::endl;
}
int dequeue() {
if (isEmpty()) {
std::cout << "Queue is Empty" << std::endl;
return -1;
}
int value = arr[front];
if (front == rear) {
front = rear = -1;
} else {
front = (front + 1) % MAX;
}
std::cout << value << " removed from the queue" << std::endl;
return value;
}
void displayQueue() {
if (isEmpty()) {
std::cout << "Queue is Empty" << std::endl;
return;
}
int i = front;
while (true) {
std::cout << arr[i] << " ";
if (i == rear) break;
i = (i + 1) % MAX;
}
std::cout << std::endl;
}
};
int main() {
CircularQueue q;
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.enqueue(40);
q.enqueue(50);
q.displayQueue();
q.dequeue();
q.displayQueue();
q.enqueue(60);
q.displayQueue();
return 0;
}
Circular queues offer an elegant solution to efficiently manage linear data structures within fixed memory limits. By reusing spaces and maintaining a circular structure, they optimize performance and memory utilization. Whether used in operating systems, data buffering, or real-time applications, understanding and implementing circular queues is essential for effective data management and system optimization.