C++ Program to Implement a Queue Using Arrays

A queue is a fundamental data structure that operates on a First In First Out (FIFO) principle. Elements are added at the rear and removed from the front. This article will guide you through the implementation of a queue using arrays in C++, providing multiple examples to demonstrate various operations and their outputs.

Prerequisites

Before diving into the implementation, you should have:

  1. Basic understanding of C++ programming.
  2. Familiarity with arrays.
  3. Knowledge of queue operations.

With these prerequisites, you will be able to grasp the concepts and code implementations discussed here.

1. Queue Implementation Using Arrays

1.1 Queue Structure and Initialization

Let’s start by defining the structure of a queue and initializing it.

C++
#include <iostream>
#define MAX_SIZE 5

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

    void enqueue(int data) {
        if (rear == MAX_SIZE - 1) {
            std::cout << "Queue is full" << std::endl;
            return;
        }
        if (front == -1) {
            front = 0;
        }
        queue[++rear] = data;
    }

    void dequeue() {
        if (front == -1 || front > rear) {
            std::cout << "Queue is empty" << std::endl;
            return;
        }
        std::cout << "Dequeued: " << queue[front++] << std::endl;
    }

    void display() {
        if (front == -1) {
            std::cout << "Queue is empty" << std::endl;
            return;
        }
        std::cout << "Queue: ";
        for (int i = front; i <= rear; i++) {
            std::cout << queue[i] << " ";
        }
        std::cout << std::endl;
    }

private:
    int queue[MAX_SIZE];
    int front, rear;
};

int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.enqueue(40);
    q.enqueue(50);

    std::cout << "Initial Queue: ";
    q.display();

    return 0;
}

1.2 Output Explanation

In this example, the queue is structured as follows:

C++
Queue: 10 20 30 40 50

The output will display: Initial Queue: 10 20 30 40 50

2. Advanced Queue Operations

2.1 Enqueue and Dequeue Operations

Let’s demonstrate the enqueue and dequeue operations with more details.

C++
int main() {
    Queue q;
    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.enqueue(40);
    q.enqueue(50);

    std::cout << "Queue after enqueuing 10, 20, 30, 40, 50: ";
    q.display();

    q.dequeue();
    q.dequeue();
    std::cout << "Queue after dequeuing two elements: ";
    q.display();

    q.enqueue(60); // This will show that the queue is full
    q.display();

    return 0;
}

2.2 Output Explanation

In this example, the queue undergoes the following transformations:

  • After enqueuing 10, 20, 30, 40, 50:Queue: 10 20 30 40 50 The output will display: Queue after enqueuing 10, 20, 30, 40, 50: 10 20 30 40 50
  • After dequeuing two elements:Dequeued: 10 Dequeued: 20 Queue: 30 40 50 The output will display: Queue after dequeuing two elements: 30 40 50
  • Attempting to enqueue 60 will result in “Queue is full” because the array’s fixed size does not allow more elements once the rear index reaches MAX_SIZE – 1.

3. Handling Edge Cases

3.1 Full and Empty Queue

Let’s handle the edge cases where the queue is full or empty.

C++
int main() {
    Queue q;

    q.dequeue();  // Attempting to dequeue from an empty queue

    q.enqueue(10);
    q.enqueue(20);
    q.enqueue(30);
    q.enqueue(40);
    q.enqueue(50);
    q.enqueue(60);  // Attempting to enqueue in a full queue

    q.display();

    return 0;
}

3.2 Output Explanation

In this example, attempting to dequeue from an empty queue and enqueue in a full queue will produce the following output:

C++
Queue is empty
Queue is full
Queue: 10 20 30 40 50

This ensures that the program handles edge cases gracefully without crashing.

Conclusion

A C++ program to implement a queue using arrays involves defining the queue structure and implementing various operations such as enqueue, dequeue, and display. This article provided a detailed implementation of these operations along with multiple examples to illustrate their functionality.