Circular queues in C++

In computer science, a queue is a data structure that follows the FIFO (First-In-First-Out) principle. In a queue, elements are added at the rear and removed from the front. However, a circular queue is a variation of a standard queue where the last element connects back to the first element, creating a circular structure.

Circular queues have several advantages over regular queues, such as efficient memory utilization and better performance in certain scenarios. In this article, we will explore circular queues and their implementation in C++.

How Circular Queues Work

A circular queue can be visualized as a circular entity where elements are stored in consecutive memory locations. When a new element is inserted, it takes the next available position. Similarly, when an element is removed, the first element is dequeued from the queue.

The key difference between a circular queue and a normal queue is that when the rear of the queue reaches the end of the allocated memory, it wraps around and starts from the beginning. This allows efficient space utilization and avoids wastage of memory.

Implementing Circular Queues in C++

To implement a circular queue in C++, we need to define a class that encapsulates the necessary functionality. Let’s call this class CircularQueue. Here’s an example implementation:

class CircularQueue {
private:
    int* queue;  // Dynamic array to store the elements
    int front;   // Index of the front element
    int rear;    // Index of the rear element
    int size;    // Maximum size of the queue
    int count;   // Current number of elements in the queue

public:
    CircularQueue(int maxSize) {
        size = maxSize;
        front = 0;
        rear = -1;
        count = 0;
        queue = new int[size];
    }
    
    ~CircularQueue() {
        delete[] queue;
    }
    
    bool isEmpty() {
        return count == 0;
    }
    
    bool isFull() {
        return count == size;
    }
    
    void enqueue(int data) {
        if (!isFull()) {
            rear = (rear + 1) % size;
            queue[rear] = data;
            count++;
        }
    }
    
    int dequeue() {
        if (!isEmpty()) {
            int data = queue[front];
            front = (front + 1) % size;
            count--;
            return data;
        }
        return -1; // Return -1 if the queue is empty
    }
};

Using the CircularQueue Class

To use the CircularQueue class, create an instance by specifying the maximum size of the queue. Then, you can enqueue and dequeue elements as needed. Here’s an example of how to use the CircularQueue class:

int main() {
    CircularQueue queue(5); // Create a circular queue with a maximum size of 5
    
    queue.enqueue(10);
    queue.enqueue(20);
    queue.enqueue(30);
    
    // Dequeue the elements and print them
    while (!queue.isEmpty()) {
        int data = queue.dequeue();
        std::cout << data << " ";
    }
    
    return 0;
}

Conclusion

Circular queues are a useful data structure that offers efficient memory utilization and better performance in certain situations compared to regular queues. By implementing a CircularQueue class in C++, we can take advantage of these benefits and easily handle enqueue and dequeue operations.

Using the example code provided, you can create and utilize circular queues in your C++ programs. Enjoy coding!

#cplusplus #datastructures