20. DSA with C++ - Queue using Array

Queue with array

This is an implementaon of a queue using arrays in C++, along with the main operaons performed on a queue:

            
#include <iostream>
class Queue {
private:
    int front; // Index of the front element
    int rear; // Index of the rear element
    int capacity; // Maximum number of elements in the queue
    int* elements; // Array to store the queue elements
public:
    Queue(int size) {
        capacity = size;
        front = -1;
        rear = -1;
        elements = new int[capacity];
    }
    ~Queue() {
        delete[] elements;
    }
    bool isEmpty() {
        return (front == -1);
    }
    bool isFull() {
        return ((rear + 1) % capacity == front);
    }
    void enqueue(int item) {
        if (isFull()) {
            std::cout << "Queue is full. Unable to enqueue element." << std::endl;
            return;
        }
        if (isEmpty()) {
            front = 0; // If the queue is empty, set the front to 0
        }
        rear = (rear + 1) % capacity; // Move the rear circularly
        elements[rear] = item; // Insert the item at the rear
        std::cout << "Enqueued element: " << item << std::endl;
    }
    void dequeue() {
        if (isEmpty()) {
            std::cout << "Queue is empty. Unable to dequeue element." << std::endl;
            return;
        }
        int item = elements[front]; // Get the front element
        if (front == rear) {
            front = -1; // If the queue has only one element, reset front and rear
            rear = -1;
        } else {
            front = (front + 1) % capacity; // Move the front circularly
        }
        std::cout << "Dequeued element: " << item << std::endl;
    }
    void display() {
        if (isEmpty()) {
            std::cout << "Queue is empty." << std::endl;
            return;
        }
        std::cout << "Queue elements: ";
        int count = (rear + capacity - front) % capacity + 1;
        int index = front;
        while (count) {
            std::cout << elements[index] << " ";
            index = (index + 1) % capacity;
            count--;
        }
        std::cout << std::endl;
    }
};
int main() {
    Queue queue(5);
    queue.enqueue(10);
    queue.enqueue(20);
    queue.enqueue(30);
    queue.enqueue(40);
    queue.enqueue(50);
    queue.enqueue(60); // Queue is full, unable to enqueue element
    queue.display(); // Queue elements: 10 20 30 40 50
    queue.dequeue();
    queue.dequeue();
    queue.dequeue();
    queue.dequeue();
    queue.dequeue();
    queue.dequeue(); // Queue is empty, unable to dequeue element
    queue.display(); // Queue is empty.
    return 0;
}
            
        
Let's go through the step-by step implementaon and the main operaons performed on the queue:
  1. Define the Queue class with private member variables: - front: Index of the front element. - rear: Index of the rear element. - capacity: Maximum number of elements in the queue. - elements: Array to store the queue elements.
  2. Implement the constructor Queue(int size) to ini alize the queue with the specified size. It sets the ini al values of front and rear to -1, indicang an empty queue, and dynamically allocates memory for the array elements.
  3. Implement the destructor ~Queue() to deallocate the memory used by the array elements when the queue object is destroyed.
  4. Implement the isEmpty() funcon to check if the queue is empty by checking if front is -1.
  5. Implement the isFull() funcon to check if the queue is full by checking if the next index of rear (taking into account the circular nature of the queue) is equal to front.
  6. Implement the enqueue(int item) funcon to insert an element into the queue. It checks if the queue is full and returns an error message if it is. Otherwise, it updates the rear index, inserts the element at the new rear index, and prints a message indicang the element that has been enqueued.
  7. Implement the dequeue() funcon to remove an element from the queue. It checks if the queue is empty and returns an error message if it is. Otherwise, it retrieves the element at the front index, updates the front index (taking into account the circular nature of the queue), and prints a message indicang the element that has been dequeued.
  8. Implement the display() funcon to traverse and display the elements of the queue. It checks if the queue is empty and returns a message if it is. Otherwise, it calculates the number of elements in the queue and uses a loop to print the elements starng from the front index and wrapping around the array if necessary.
  9. In the main() funcon, we create a Queue object with a capacity of 5.
  10. We enqueue five elements into the queue and aempt to enqueue a sixth element (which results in an error message).
  11. We display the elements of the queue.
  12. We dequeue all the elements from the queue and aempt to dequeue an addional element (which results in an error message).
  13. We display the elements of the queue again (which should show an empty queue message).
Output
    
Enqueued element: 10 
Enqueued element: 20 
Enqueued element: 30 
Enqueued element: 40 
Enqueued element: 50 
Queue is full. Unable to enqueue element. 
Queue elements: 10 20 30 40 50 
Dequeued element: 10 
Dequeued element: 20 
Dequeued element: 30 
Dequeued element: 40 
Dequeued element: 50 
Queue is empty. Unable to dequeue element. 
Queue is empty.