A circular queue is a linear data structure that follows the First In First Out (FIFO) principle but unlike a traditional queue, the last position is connected back to the first position to make a circle. It is very useful for managing resources in computing environments where the system needs to run continuously.

Program Code

#include <iostream>
using namespace std;

class CircularQueue {
private:
    int front, rear, size;
    int *queue;

public:
    CircularQueue(int size) {
        this->size = size;
        queue = new int[size];
        front = rear = -1;
    }

    ~CircularQueue() {
        delete[] queue;
    }

    // Function to insert an element at the rear of the queue
    void enqueue(int value) {
        if ((front == 0 && rear == size - 1) || (rear == (front - 1) % (size - 1))) {
            cout << "Queue is Full\n";
        } else if (front == -1) { // Insert First Element
            front = rear = 0;
            queue[rear] = value;
        } else if (rear == size - 1 && front != 0) {
            rear = 0;
            queue[rear] = value;
        } else {
            rear++;
            queue[rear] = value;
        }
    }

    // Function to delete an element from the front of the queue
    void dequeue() {
        if (front == -1) {
            cout << "Queue is Empty\n";
        } else if (front == rear) {
            cout << "Element " << queue[front] << " is removed\n";
            front = rear = -1;
        } else if (front == size - 1) {
            cout << "Element " << queue[front] << " is removed\n";
            front = 0;
        } else {
            cout << "Element " << queue[front] << " is removed\n";
            front++;
        }
    }

    // Function to display the elements of the queue
    void displayQueue() {
        if (front == -1) {
            cout << "Queue is Empty\n";
            return;
        }
        cout << "Elements in the Circular Queue are: ";
        if (rear >= front) {
            for (int i = front; i <= rear; i++)
                cout << queue[i] << " ";
        } else {
            for (int i = front; i < size; i++)
                cout << queue[i] << " ";
            for (int i = 0; i <= rear; i++)
                cout << queue[i] << " ";
        }
        cout << endl;
    }
};

int main() {
    CircularQueue q(5);

    // Enqueue elements
    q.enqueue(14);
    q.enqueue(22);
    q.enqueue(13);
    q.enqueue(-6);
    q.enqueue(25);

    q.displayQueue();

    q.dequeue();
    q.displayQueue();

    q.enqueue(23);
    q.displayQueue();

    return 0;
}

Class and Function Documentation

  • Constructor CircularQueue(int size): Initializes the queue of given maximum size.
  • void enqueue(int value): Adds a value to the rear of the queue. If the queue is full, it will output “Queue is Full”.
  • void dequeue(): Removes the front item of the queue and prints it. If the queue is empty, it will output “Queue is Empty”.
  • void displayQueue(): Prints all elements in the queue from front to rear. This method demonstrates the circular nature of the queue if it wraps around.

Example Usage

    CircularQueue q(5);
    q.enqueue(14);
    q.enqueue(22);
    q.enqueue(13);
    q.dequeue();
    q.displayQueue();  // Output will show remaining elements in the queue

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)