Implement a Circular Queue and Perform Basic Operations in C++

In this C++ exercise, you will implement a circular queue data structure. A circular queue is a type of queue where the last element points to the first element, forming a circular structure. This is useful for efficiently managing data where the queue operates in a circular manner, such as in resource management. The exercise will involve implementing the basic operations such as enqueue (insertion), dequeue (removal), and checking if the queue is full or empty.

Group

Data Structures: Stacks, Queues in C++

Objective

1. Create a class to implement a circular queue with a fixed size.
2. Implement the following operations:
- `enqueue`: Add an element to the queue.
- `dequeue`: Remove an element from the queue.
- `isFull`: Check if the queue is full.
- `isEmpty`: Check if the queue is empty.
3. Use a circular array to represent the queue, with pointers for the front and rear elements.
4. Test the queue by performing the operations and printing the results.

Develop a circular queue and perform the corresponding operations.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include for input/output operations
using namespace std;

class CircularQueue {
private:
    int* queue;  // Pointer to the array that holds the queue elements
    int front;   // Index for the front of the queue
    int rear;    // Index for the rear of the queue
    int capacity;  // Maximum size of the queue
    int size;  // Current size of the queue

public:
    // Constructor to initialize the queue with a fixed capacity
    CircularQueue(int cap) {
        capacity = cap;  // Set the capacity
        queue = new int[capacity];  // Allocate memory for the queue
        front = 0;  // Initialize front to 0
        rear = -1;  // Initialize rear to -1 (queue is empty)
        size = 0;  // Initialize size to 0
    }

    // Function to check if the queue is full
    bool isFull() {
        return size == capacity;  // Queue is full if size equals capacity
    }

    // Function to check if the queue is empty
    bool isEmpty() {
        return size == 0;  // Queue is empty if size is 0
    }

    // Function to add an element to the queue
    void enqueue(int element) {
        if (isFull()) {  // Check if the queue is full
            cout << "Queue is full! Cannot enqueue." << endl;
            return;  // Do nothing if the queue is full
        }
        rear = (rear + 1) % capacity;  // Update rear (circular increment)
        queue[rear] = element;  // Insert the new element
        size++;  // Increment the size of the queue
        cout << "Enqueued: " << element << endl;
    }

    // Function to remove an element from the queue
    void dequeue() {
        if (isEmpty()) {  // Check if the queue is empty
            cout << "Queue is empty! Cannot dequeue." << endl;
            return;  // Do nothing if the queue is empty
        }
        cout << "Dequeued: " << queue[front] << endl;  // Print the dequeued element
        front = (front + 1) % capacity;  // Update front (circular increment)
        size--;  // Decrement the size of the queue
    }

    // Function to display the queue elements
    void display() {
        if (isEmpty()) {  // Check if the queue is empty
            cout << "Queue is empty!" << endl;
            return;  // Do nothing if the queue is empty
        }
        cout << "Queue elements: ";
        for (int i = 0; i < size; i++) {
            cout << queue[(front + i) % capacity] << " ";  // Print elements in circular order
        }
        cout << endl;
    }
};

// Main function to test the CircularQueue class
int main() {
    CircularQueue q(5);  // Create a circular queue with capacity 5

    q.enqueue(10);  // Enqueue elements
    q.enqueue(20);
    q.enqueue(30);
    q.enqueue(40);
    q.enqueue(50);

    q.display();  // Display the queue

    q.enqueue(60);  // Attempt to enqueue when the queue is full

    q.dequeue();  // Dequeue elements
    q.dequeue();

    q.display();  // Display the queue after dequeue operations

    q.enqueue(60);  // Enqueue new elements after dequeue
    q.enqueue(70);

    q.display();  // Display the final state of the queue

    return 0;
}

 Output

Enqueued: 10
Enqueued: 20
Enqueued: 30
Enqueued: 40
Enqueued: 50
Queue elements: 10 20 30 40 50
Queue is full! Cannot enqueue.
Dequeued: 10
Dequeued: 20
Queue elements: 30 40 50
Enqueued: 60
Enqueued: 70
Queue elements: 30 40 50 60 70

Share this C++ Exercise


More C++ Programming Exercises of Data Structures: Stacks, Queues in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.