Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

circular queue based on array/size of array is 5 it is not showing q is full after inserting more than 5 elements

+1 vote
asked Oct 22, 2021 by Tarun Kumar Tarlana (350 points)

#include<iostream>

using namespace std;

class qu{

    int front,rear,size;

    int *arr;

    public:

        qu(int n)

        {

            front=-1;

            rear=-1;

            arr=new int[n];

            size=n;

            cout<<size<<endl;

        }

       

     

        bool isfull()

        {

            if(rear+1%size ==front)

              {

              return true;

              }

            else

            {

               return false;

            }

        }

        bool isempty()

        {  

            if(front==-1)

             return true;

             else

             return false;

        }

        void enque(int x)

        {

            if(isfull())

            {

                cout<<"q is full"<<endl;

            }

            else if(isempty())

            {

                 rear=0;front=0;

                 arr[rear]=x;

            }

            else{

                rear=rear+1%size;

                arr[rear]=x;

            }

        }

        void deque()

        {  

            if(isempty())

            {

               cout<<"q is empty"<<endl;  

            }

            else if(front==rear)

            {   cout<<arr[front]<<endl;

                front=-1;rear=-1;

            }

            else

            {

                cout<<arr[front]<<endl;

                front=front+1%size;

            }

        }

       

        void display()

        {  if(front!=-1)

           {

           int  x=front;

            while(x!=rear)

            {

                cout<<arr[x]<<" ";

                x=x+1%size;

            }

            cout<<arr[rear]<<endl;

           }

           else{

               cout<<"q is empty\n";

           }

        }

};

qu q(5);

int main()

{

    q.deque();

    q.enque(1);

    q.enque(2);

    q.enque(3);

    q.enque(4);

    q.enque(5);

    //i  inserted  5 elements

    q.enque(6);//trying to insert 6th element  ,size of q is 5 only// but it is not returning q is full

    q.enque(7);

    q.display();

    q.deque();

    q.deque();

    q.deque();

    q.display();

    q.enque(200);

    q.enque(201);

    q.enque(203);

    q.display();

   

}

1 Answer

0 votes
answered Oct 23, 2021 by Peter Minarik (86,200 points)
edited Oct 23, 2021 by Peter Minarik

Solving your issue

Your problem is that you assumed incorrect operator precedence. Since modulo division (%) has higher precedence (priority) than addition (+) your expression

rear + 1 % size

is not evaluated as (you assumed)

(rear + 1) % size

but

rear + (1 % size)

If you apply parenthesis to ensure increment to be calculated first only after then the modulo division then your code works correctly (2nd code snippet).

And a bit more...

I took some time to enhance your code.
  • added a destructor to properly free the resources
  • used initialization list in the constructor
  • did some name refactoring on the code (functions to follow upper CamelCase, some names changed, etc)
  • made functions constant when they do not change the instance
  • used generics to allow the RingBuffer to store any arbitrary type
  • used throw to indicate errors (such as full/empty RingBuffer when trying to enqueue/dequeue)
So here's the enhanced version of mine:
#include <iostream>
#include <stdexcept>

template <typename X> class RingBuffer
{
private:
    size_t _size;
    int _front;
    int _rear;
    X * _elements;

public:
    RingBuffer(size_t size) :
        _size(size),
        _front(-1),
        _rear(-1),
        _elements(new X[size])
    { }
    
    ~RingBuffer() { delete[] _elements; }

    bool IsFull() const { return (_rear + 1) % (int)_size == _front; }

    bool IsEmpty() const { return _front == -1; }

    void Enqueue(X x)
    {
        if (IsFull())
            throw std::runtime_error("RingBUffer is full");

        if (IsEmpty())
        {
            _rear = 0;
            _front = 0;
        }
        else
        {
            _rear = (_rear + 1) % _size;
        }
        
        _elements[_rear] = x;
    }

    X Dequeue()
    {  
        if (IsEmpty())
            throw std::runtime_error("RingBUffer is empty");

        int index = _front;
        if (_front == _rear)
        {
            _front = -1;
            _rear = -1;
        }
        else
        {
            _front = (_front + 1) % _size;
        }
        
        return _elements[index];
    }

    void PrintElement(int i) const
    {
        if (i != _front)
            std::cout << ", ";
        std::cout << _elements[i];
    }

    void Display() const
    {
        if (IsEmpty())
        {
            std::cout << "RingBuffer is empty." << std::endl;
            return;
        }
        
        for (int i = _front; i != _rear; i = (i + 1) % _size)
            PrintElement(i);

        PrintElement(_rear);
        std::cout << std::endl;
    }
};

int main()
{
    RingBuffer<int> ringBuffer(5);
    try
    {
        ringBuffer.Dequeue();
    }
    catch (const std::runtime_error & error)
    {
        std::cerr << error.what() << std::endl;
    }
    ringBuffer.Enqueue(1);
    ringBuffer.Enqueue(2);
    ringBuffer.Enqueue(3);
    ringBuffer.Enqueue(4);
    ringBuffer.Enqueue(5);
    // inserted 5 elements

    try
    {
        ringBuffer.Enqueue(6); // trying to insert 6th element, RingBuffer is full
    }
    catch (const std::runtime_error & error)
    {
        std::cerr << error.what() << std::endl;
    }

    try
    {
        ringBuffer.Enqueue(7);
    }
    catch (const std::runtime_error & error)
    {
        std::cerr << error.what() << std::endl;
    }
    ringBuffer.Display();
    ringBuffer.Dequeue();
    ringBuffer.Dequeue();
    ringBuffer.Dequeue();
    ringBuffer.Display();
    ringBuffer.Enqueue(200);
    ringBuffer.Enqueue(201);
    ringBuffer.Enqueue(203);
    ringBuffer.Display();
    
    return 0;
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...