Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to implement a circular queue (perform rotation on an array)

ID: 3733029 • Letter: W

Question

Write a C++ program to implement a circular queue (perform rotation on an array). 10 customers are sharing a resource and they are allocated 10 minutes each time it is their turn (They are allocated the 10 minutes when they are in the location 0 of the array int service[10];) . Fill the array with the following customer IDs: 10, 20, 30, 40, 50, 60, 70, 80, 90 , 100

Each time you perform a rotation, print the content of the array and indicate that the customer in the first cell is the one using the resource.

Once finished, copy your code and PrtScn of output to a word document and upload to this drop box.

Explanation / Answer

    /*
     * C++ program to implement a circular queue (perform rotation on an array)
     */

#include <iostream>
#define MAX 10

using namespace std;

/*
* Class Circular Queue
*/
class Circular_Queue
{
private:
int *cqueue_arr;
int front, rear;

public:
Circular_Queue()
{
    cqueue_arr = new int [MAX];
    rear = front = -1;
}

/*
   * Insert into Circular Queue
   */

void insert(int item)
{
    if ((front == 0 && rear == MAX-1) || (front == rear+1))
      {
   cout<<"Queue Overflow ";
   return;
      }
    if (front == -1)
      {  
   front = 0;
   rear = 0;
      }
    else
      {
   if (rear == MAX - 1)
      rear = 0;
   else
      rear = rear + 1;
      }
    cqueue_arr[rear] = item ;
}

/*
   * Delete from Circular Queue
   */
void del()
{
    if (front == -1)
      {
   cout<<"Queue Underflow ";
   return ;
      }
    cout<<"Element deleted from queue is : "<<cqueue_arr[front]<<endl;
    if (front == rear)
      {
   front = -1;
   rear = -1;
      }
    else
      {
   if (front == MAX - 1)
      front = 0;
   else
      front = front + 1;
      }
}

/*
   * Display Circular Queue
   */
void display()
{
    int front_pos = front, rear_pos = rear;
    if (front == -1)
      {
   cout<<"Queue is empty ";
   return;
      }
    cout<<"Queue elements : ";
    if (front_pos <= rear_pos)
      {
   while (front_pos <= rear_pos)
      {
        cout<<cqueue_arr[front_pos]<<" ";
        front_pos++;
      }
      }
    else
      {
   while (front_pos <= MAX - 1)
      {
        cout<<cqueue_arr[front_pos]<<" ";
        front_pos++;
      }
   front_pos = 0;
   while (front_pos <= rear_pos)
      {
        cout<<cqueue_arr[front_pos]<<" ";
        front_pos++;
      }
      }
    cout<<endl;
}

/*Function to left rotate circular queue of size n by d*/
void leftRotatebyOne( int n)
{
int i, temp;
temp = cqueue_arr[0];
for (i = 0; i < n-1; i++)
     cqueue_arr[i] = cqueue_arr[i+1];
cqueue_arr[i] = temp;
}
void leftRotate(int d, int n)
{
int i;
for (i = 0; i < d; i++)
    leftRotatebyOne(n);
}


};


/*
* Main
*/
int main()
{
int choice, item,i;
int cust_ids[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90 , 100};
Circular_Queue cq;

for (i=0;i<10;i++)
{
     cq.insert(cust_ids[i]);
}
cq.display();
cout<<" ------------------------------------------------------ ";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";
cq.leftRotate(1, 10);
cq.display();
cout<<" ------------------------------------------------------";

return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote