Exam 1 - Question 3

 avatar
itsLu
c_cpp
2 months ago
2.1 kB
19
Indexable
Never
#include <iostream>
using namespace std;

class queueUsingArray
{
    int * queueArray , sizeOfQueue , front , rear ,counter;
public:
    queueUsingArray(int sizeOfArray = 60)
    {
        sizeOfQueue = sizeOfArray;
        queueArray = new int[sizeOfQueue];
        front = rear = counter = 0;
    }
    bool isFull()
    {
        return counter == sizeOfQueue;
    }
    void doubleSize()
    {
        int * tempArray = new int[sizeOfQueue * 2];
        for(int i =0 ; i < sizeOfQueue ; i++)
            tempArray[i] = queueArray[(i + front) % sizeOfQueue];
        delete[] queueArray;
        sizeOfQueue *= 2;
        queueArray = tempArray;
        front = 0;
        rear = counter;
    }
    void enqueue (int data)
    {
        if(isFull())
        {
            cout << "The queue is Full!\nDoubling the size...\n";
            doubleSize();
        }
        queueArray[rear] = data;
        rear = (rear + 1) % sizeOfQueue;
        counter++;
    }
    bool isEmpty()
    {
        return counter == 0;
    }
    int dequeue()
    {
        if(isEmpty())
        {
            cout << "Queue is empty!\n";
            return -1;
        }
        int temp = front;
        front = (front + 1) % sizeOfQueue;
        counter--;
        return queueArray[temp];
    }
    void displayQueue()
    {
        if(isEmpty())
            cout << "Queue is empty!\n";
        else
        {
            for(int i = 0 ; i < counter ; i++)
                cout << queueArray[(i + front) % sizeOfQueue] << "\t";
        }
    }
    ~queueUsingArray()
    {
        delete[] queueArray;
    }
};


int main()
{
    queueUsingArray oddQueue , evenQueue;
    int temp;
    cout << "Please enter 100 numbers: \n";
    for (int i = 0 ; i < 100 ; i++)
    {
        cin >> temp;
        if(temp % 2 == 0)
            evenQueue.enqueue(temp);
        else
            oddQueue.enqueue(temp);
    }
    cout << "\nOddQueue: \n";
    oddQueue.displayQueue();
    cout << "\nEvenQueue: \n";
    evenQueue.displayQueue();
    return 0;
}
Leave a Comment