This C++ program demonstrates how to implement a queue using an array. A queue is a FIFO (First In First Out) data structure used in many programming and system applications.

Program Code

#include <iostream>
#define SIZE 5

class Queue {
private:
    int items[SIZE], front, rear;

public:
    Queue() : front(-1), rear(-1) {}

    bool isFull() {
        return (rear + 1) % SIZE == front;
    }

    bool isEmpty() {
        return front == -1;
    }

    void enqueue(int element) {
        if (isFull()) {
            std::cout << "Queue is full" << std::endl;
        } else {
            if (front == -1) front = 0;
            rear = (rear + 1) % SIZE;
            items[rear] = element;
            std::cout << "Inserted " << element << std::endl;
        }
    }

    int dequeue() {
        if (isEmpty()) {
            std::cout << "Queue is empty" << std::endl;
            return -1;
        } else {
            int element = items[front];
            if (front == rear) {
                front = -1;
                rear = -1;
            } else {
                front = (front + 1) % SIZE;
            }
            return element;
        }
    }

    void display() {
        if (isEmpty()) {
            std::cout << "Queue is empty" << std::endl;
        } else {
            std::cout << "Front index-> " << front << std::endl;
            std::cout << "Items -> ";
            for (int i = front; i != rear; i = (i + 1) % SIZE) {
                std::cout << items[i] << " ";
            }
            std::cout << items[rear] << std::endl;
            std::cout << "Rear index-> " << rear << std::endl;
        }
    }
};

int main() {
    Queue q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.enqueue(4);
    q.enqueue(5);
    q.enqueue(6); // Queue is full
    q.display();

    int elem = q.dequeue();
    std::cout << "Deleted Element -> " << elem << std::endl;
    q.display();

    q.enqueue(7);
    q.display();

    return 0;
}

Class and Function Documentation

  • Constructor Queue(): Initializes a new queue with a fixed size defined by the SIZE macro.
  • bool isFull(): Checks if the queue is full. Returns true if full, false otherwise.
  • bool isEmpty(): Checks if the queue is empty. Returns true if empty, false otherwise.
  • void enqueue(int element): Adds an element to the rear of the queue if it is not full, otherwise outputs an error message.
  • int dequeue(): Removes and returns the front element of the queue. If the queue is empty, returns -1 and outputs an error message.
  • void display(): Displays all elements from front to rear, also showing the front and rear indices, useful for debugging and understanding the internal state of the queue.

Example Usage

    Queue q;
    q.enqueue(1);
    q.enqueue(2);
    q.enqueue(3);
    q.display(); // Outputs the contents of the queue and the front and rear indices
    int elem = q.dequeue();
    std::cout << "Deleted Element -> " << elem << std::endl; // Outputs the deleted element

 

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 :)