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

I need help with a program in c++. A circular queue (https://en.wikipedia.org/wi

ID: 3728203 • Letter: I

Question

I need help with a program in c++.

A circular queue (https://en.wikipedia.org/wiki/Circular_buffer) is one way to implement a queue in the computer. In this assignment, you will implement a CircularQueue class that stores chars, with the following characteristics A circular queue is stored in an array In addition to the array, the data structure keeps track two indices in the array: a 'start" position, and an end position. When an item is enqueued (added) to the queue, it is added at the end position. The end position is then incremented, wrapping around to zero it reaches the end of the array To dequeue (remove and return) an item from the queue, we return the item at the start position, and increment the start position (once again wrapping around when it reaches the end of the array). If start and end are the same, then the queue is empty. · if ( end+1 ) % arrayLength-start, then the queue is full. To add another element to the queue, a new array must be allocated with twice the size of the current one, and all of the elements copied to it, starting at position 0. In C++, after the copying, the old array should be deleted to avoid a memory leak Write a class called CircularQueue, or something similar, that implements a circular queue for chars as described above, using an array of chars as the array storage. The queue should start out with an array of length 2. Write a program that uses your CircularQueue class to answer queries that either add letters to the queue, or remove them from the queue. When a letter is removed from the queue, print out both the letter's position in the array, and the letter itself.

Explanation / Answer

// C or C++ program for insertion and
// deletion in Circular Queue
#include<bits/stdc++.h>
using namespace std;

struct Queue
{
// Initialize front and rear
int rear, front;

// Circular Queue
int size;
int *arr;

Queue(int s)
{
front = rear = -1;
size = s;
arr = new int[s];
}

void enQueue(int value);
int deQueue();
void displayQueue();
};


/* Function to create Circular queue */
void Queue::enQueue(int value)
{
if ((front == 0 && rear == size-1) ||
(rear == front-1))
{
printf(" Queue is Full");
return;
}

else if (front == -1) /* Insert First Element */
{
front = rear = 0;
arr[rear] = value;
}

else if (rear == size-1 && front != 0)
{
rear = 0;
arr[rear] = value;
}

else
{
rear++;
arr[rear] = value;
}
}

// Function to delete element from Circular Queue
int Queue::deQueue()
{
if (front == -1)
{
printf(" Queue is Empty");
return INT_MIN;
}

int data = arr[front];
arr[front] = -1;
if (front == rear)
{
front = -1;
rear = -1;
}
else if (front == size-1)
front = 0;
else
front++;

return data;
}

// Function displaying the elements
// of Circular Queue
void Queue::displayQueue()
{
if (front == -1)
{
printf(" Queue is Empty");
return;
}
printf(" Elements in Circular Queue are: ");
if (rear >= front)
{
for (int i = front; i <= rear; i++)
printf("%d ",arr[i]);
}
else
{
for (int i = front; i < size; i++)
printf("%d ", arr[i]);

for (int i = 0; i <= rear; i++)
printf("%d ", arr[i]);
}
}

/* Driver of the program */
int main()
{
Queue q(5);

// Inserting elements in Circular Queue
q.enQueue(14);
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);

// Display elements present in Circular Queue
q.displayQueue();

// Deleting elements from Circular Queue
printf(" Deleted value = %d", q.deQueue());
printf(" Deleted value = %d", q.deQueue());

q.displayQueue();

q.enQueue(9);
q.enQueue(20);
q.enQueue(5);

q.displayQueue();

q.enQueue(20);
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