Followers

Wednesday, July 17, 2024

An implementation of queue operations using an array in C++


 

#include <iostream>

using namespace std;

class Queue {

private:

    int front, rear, size;

    int* queueArray;

    int capacity;

public:

    Queue(int capacity) {

        this->capacity = capacity;

        queueArray = new int[capacity];

        front = size = 0;

        rear = capacity - 1;

    }

    ~Queue() {

        delete[] queueArray;

    }

    bool isFull() {

        return (size == capacity);

    }

    bool isEmpty() {

        return (size == 0);

    }

    void enqueue(int item) {

        if (isFull()) {

            cout << "Queue overflow! Cannot enqueue " << item << endl;

            return;

        }

        rear = (rear + 1) % capacity;

        queueArray[rear] = item;

        size++;

        cout << item << " enqueued to queue" << endl;

    }

    int dequeue() {

        if (isEmpty()) {

            cout << "Queue underflow! Cannot dequeue" << endl;

            return -1; // Assuming -1 as error value for queue underflow

        }

        int item = queueArray[front];

        front = (front + 1) % capacity;

        size--;

        return item;

    }


    int frontElement() {

        if (isEmpty()) {

            cout << "Queue is empty! Cannot retrieve front element" << endl;

            return -1; // Assuming -1 as error value for empty queue

        }

        return queueArray[front];

    }

    int rearElement() {

        if (isEmpty()) {

            cout << "Queue is empty! Cannot retrieve rear element" << endl;

            return -1; // Assuming -1 as error value for empty queue

        }

        return queueArray[rear];

    }

};

int main() {

    int capacity;

    cout << "Enter the capacity of the queue: ";

    cin >> capacity;

    Queue queue(capacity);

    queue.enqueue(10);

    queue.enqueue(20);

    queue.enqueue(30);

    queue.enqueue(40);

    cout << "Front element is: " << queue.frontElement() << endl;

    cout << "Rear element is: " << queue.rearElement() << endl;

    cout << "Dequeued element: " << queue.dequeue() << endl;

    cout << "Front element after dequeue: " << queue.frontElement() << endl;

    // Attempt to dequeue from empty queue

    while (!queue.isEmpty()) {

        queue.dequeue();

    }

    queue.dequeue();

    return 0;

}


Explanation:

  1. Queue Class:

    • Contains a constructor to initialize the queue with a given capacity.
    • Contains a destructor to deallocate the memory used by the queue.
    • Implements isFull to check if the queue is full.
    • Implements isEmpty to check if the queue is empty.
    • Implements enqueue to add an element to the queue.
    • Implements dequeue to remove and return the front element from the queue.
    • Implements frontElement to return the front element without removing it.
    • Implements rearElement to return the rear element without removing it.
  2. Main Function:

    • Takes the capacity of the queue from the user.
    • Demonstrates basic queue operations (enqueue, dequeue, frontElement, rearElement).

No comments:

Post a Comment