Implementing a Queue using an Array in C++

 

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

 

Leave a Reply

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