Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Moving first element to the back in Queue

I implemented the queue on my own. Also did rotate method which has to move n elements from the beginning to the end of the queue. But I missed some points and can not figure out would should I do exactly. Do you have any suggestions?

I really appreciate any help you can provide.

My output is:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

3, 4, 5, 6, 7,
4, 5, 6, 0, 3,
6, 0, 0, 0, 5,

But instead, it should look like this:

3, 4, 5, 6, 7,
4, 5, 6, 7, 3,
5, 6, 7, 3, 4,
struct Queue
{
private:
    int *data = nullptr;
    int capacity = 100;
    int counter = 0;
    int first = 0;
    int aftr = 0;

public:
    void push_back(int value)
    {
        if (aftr == capacity)
        {
            if (counter < capacity)
            {
                for (int i = 0; i < counter; i++)
                {
                    data[i] = data[first + i];
                }
                first = 0;
                aftr = counter;
            }
        }
        else
        {
            capacity = capacity * 2;
            int *tmp = new int[capacity];
            for (int i = 0; i < counter; i++)
                tmp[i] = data[i];
            delete[] data;
            data = tmp;
        }

        data[aftr] = value;
        aftr++;
        counter++;
    }
    bool empty()
    {
        return counter == 0;
    }
    int pop_front()
    {
        if (counter == 0)
        {
            std::cout << "Queue is empty" << std::endl;
        }
        int value = data[first];
        first++;
        counter--;
        return value;
    }

    void print()
    {
        if (counter == 0)
        {
            std::cout << "Empty queue" << std::endl;
        }
        else
        {
            for (int i = 0; i < counter; i++)
            {
                std::cout << data[first + i] << ", ";
            }

            std::cout << std::endl;
        }
    }
    int front()
    {
        if (counter == 0)
            std::cout << "Queue is empty" << std::endl;

        int firstElement = data[first];
        return firstElement;
    }
    int back()
    {
        if (counter == 0)
            std::cout << ("Queue is empty") << std::endl;
        int lastElement = data[aftr - 1];
        return lastElement;
    }

    void rotate(int n)
    {
        for (int i = 0; i < n; i++)
        {
            const int tmp = front();
            pop_front();
            push_back(tmp);
        }
    }
};

int main()
{
    Queue q;
    q.push_back(3);
    q.push_back(4);
    q.push_back(5);
    q.push_back(6);
    q.push_back(7);

    q.print();
    q.rotate(1);
    q.print();
    q.rotate(2);
    q.print();
}

>Solution :

Your code has issue with recurring memory allocation when push_back function is called. Condition check if(aftr == capacity) is always false.

It is better to allocated predefined memory during class constructor.

Here is the altered snippet. DEMO

struct Queue
{
private:
    constexpr static int initialCapacity = 100;
    int capacity = 0;
    int *data = nullptr;
    int counter = 0;
    int first = 0;
    int aftr = 0;

public:

    Queue():data{new int[initialCapacity]},capacity{initialCapacity}{}

    void push_back(int value)
    {
        if ((aftr == capacity) && (counter < capacity))
        {
            for (int i = 0; i < counter; i++)
            {
                data[i] = data[first + i];
            }
            first = 0;
            aftr = counter;
        }
        else if(counter == capacity)
        //^^^ here memory allocation happens only when current queue memory exhausted
        {
            capacity = capacity * 2;
            int *tmp = new int[capacity];
            for (int i = 0; i < counter; i++)
                tmp[i] = data[i];
            delete[] data;
            data = tmp;
        }

        data[aftr] = value;
        aftr++;
        counter++;
    }
  //rest of the code follows here
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading