page5 q1

 avatar
Sameh
c_cpp
2 months ago
1.8 kB
2
Indexable
Never
#include <iostream>

using namespace std;

class queueUsingArray
{
    int * queueArray , sizeOfQueue , front , rear ,counter;
public:
    queueUsingArray(int sizeOfArray = 1)
    {
        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())
            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];
    }
    int getLength()
    {
        return counter;
    }
    void printQueue()
    {
        if(isEmpty())
            cout << "Queue is empty\n";
        else
        {
            for(int i = 0 ; i < counter ; i++)
                cout << queueArray[(i + front) % sizeOfQueue] << "\t";
        }
    }
};

int main()
{
    queueUsingArray queue1;
    int temp;
    for(int i = 0 ; i < 30 ; i++)
    {
        cin >> temp;
        queue1.enqueue(temp);
    }
    queue1.printQueue();
}
Leave a Comment