Sunday, November 27, 2022
HomeSoftware DevelopmentHow one can handle Full Round Queue occasion?

How one can handle Full Round Queue occasion?


  

#embrace <bits/stdc++.h>

utilizing namespace std;

  

class Queue {

    

    int rear, entrance;

  

    

    int dimension;

    int* arr;

  

public:

    Queue(int s)

    {

        entrance = rear = -1;

        dimension = s;

        arr = new int[s];

    }

  

    void enQueue(int worth);

    int deQueue();

    void displayQueue();

};

  

void Queue::enQueue(int worth)

{

    if ((entrance == 0 && rear == dimension - 1)

        || (rear == (entrance - 1) % (dimension - 1))) {

        printf("nQueue is Full");

        return;

    }

  

    

    else if (entrance == -1) {

        entrance = rear = 0;

        arr[rear] = worth;

    }

  

    else if (rear == dimension - 1 && entrance != 0) {

        rear = 0;

        arr[rear] = worth;

    }

  

    else {

        rear++;

        arr[rear] = worth;

    }

}

  

int Queue::deQueue()

{

    if (entrance == -1) {

        printf("nQueue is Empty");

        return INT_MIN;

    }

  

    int knowledge = arr[front];

    arr[front] = -1;

    if (entrance == rear) {

        entrance = -1;

        rear = -1;

    }

    else if (entrance == dimension - 1)

        entrance = 0;

    else

        entrance++;

  

    return knowledge;

}

  

void Queue::displayQueue()

{

    if (entrance == -1) {

        printf("nQueue is Empty");

        return;

    }

    printf("nElements in Round Queue are: ");

    if (rear >= entrance) {

        for (int i = entrance; i <= rear; i++)

            printf("%d ", arr[i]);

    }

    else {

        for (int i = entrance; i < dimension; i++)

            printf("%d ", arr[i]);

  

        for (int i = 0; i <= rear; i++)

            printf("%d ", arr[i]);

    }

}

  

int predominant()

{

    Queue q(5);

  

    

    q.enQueue(14);

    q.enQueue(22);

    q.enQueue(13);

    q.enQueue(-6);

  

    

    q.displayQueue();

  

    

    printf("nDeleted worth = %d", q.deQueue());

    printf("nDeleted worth = %d", q.deQueue());

  

    q.displayQueue();

  

    q.enQueue(9);

    q.enQueue(20);

    q.enQueue(5);

  

    q.displayQueue();

  

    q.enQueue(20);

  

    return 0;

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments