Answer to Question #280424 in C++ for bks

Question #280424

Write a C++ program that outputs the following elements in the diagram using Queue data structure

1
Expert's answer
2021-12-16T14:31:17-0500
#include <iostream>


constexpr int Max_Size = 5;


class queue
{
private:
    int front;
    int back;
    int arr[Max_Size] = {};
public:
    queue() : front(-1), back(0) {}
    bool is_full()
    {
        if (back == Max_Size - 1)
        {
            return true;
        }
        else return false;
    }


    bool is_empty()
    {
        if (back == Max_Size - 1)
        {
            return true;
        }
        else return false;
    }


    void add_item(int item)
    {
        if (this->is_full())
        {
            std::cout << "Queue is full" << std::endl;
            return;
        }
        front++;
        arr[front] = item;
    }
    int pop_item()
    {
        if (this->is_empty())
        {
            std::cout << "Queue is empty" << std::endl;
            return 0;
        }
        int return_value = arr[back];
        for (int i = 0; i != Max_Size - 1; ++i)
        {
            arr[i] = arr[i + 1];
        }
        return return_value;
    }


};


int main()
{
    queue q;
    q.add_item(1);
    q.add_item(2);
    q.add_item(3);
    q.add_item(4);
    q.add_item(5);
    std::cout << q.pop_item() << std::endl;
    std::cout << q.pop_item() << std::endl;
    std::cout << q.pop_item() << std::endl;
    std::cout << q.pop_item() << std::endl;
    std::cout << q.pop_item() << std::endl;


    return 0;


}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog