16.11 Homework 6b: Circular Queue A circular queue (https://en.wikipedia.org/wik
ID: 3703478 • Letter: 1
Question
16.11 Homework 6b: Circular Queue 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 itselfExplanation / Answer
Source Code
#include<bits/stdc++.h>
using namespace std;
struct Queue
{
int r, f,size;
int *arry;
Queue(int s)
{
f = r = -1;
size = s;
arry = new int[s];
}
void enQueue(int value);
int deQueue();
void displayQ();
};
void Queue::enQueue(int value)
{
if ((f == 0 && r == size-1) ||
(r == f-1))
{
printf(" Queue is Full");
return;
}
else if (f == -1)
{
f = r = 0;
arry[r] = value;
}
else if (r == size-1 && f != 0)
{
r = 0;
arry[r] = value;
}
else
{
r++;
arry[r] = value;
}
}
int Queue::deQueue()
{
if (f == -1)
{
printf(" Queue is Empty");
return INT_MIN;
}
int data = arry[f];
arry[f] = -1;
if (f == r)
{
f = -1;
r = -1;
}
else if (f == size-1)
f = 0;
else
f++;
return data;
}
void Queue::displayQ()
{
if (f == -1)
{
printf(" Queue is Empty");
return;
}
printf(" Elements in Circular Queue are: ");
if (r >= f)
{
for (int i = f; i <= r; i++)
printf("%d ",arry[i]);
}
else
{
for (int i = f; i < size; i++)
printf("%d ", arry[i]);
for (int i = 0; i <= r; i++)
printf("%d ", arry[i]);
}
}
int main()
{
Queue q(5);
q.enQueue(14);
q.enQueue(22);
q.enQueue(13);
q.enQueue(-6);
q.displayQ(); printf(" Deleted value = %d", q.deQueue());
printf(" Deleted value = %d", q.deQueue());
q.displayQ();
q.enQueue(9);
q.enQueue(20);
q.enQueue(5);
q.displayQ();
q.enQueue(20);
return 0;
}
Output:
Elements in Circular Queue are: 14 22 13 -6
Deleted value = 14
Deleted value = 22
Elements in Circular Queue are: 13 -6
Elements in Circular Queue are: 13 -6 9 20 5
Queue is Full
I am providing the static insertion and deletion please comment if you need it dynamic insertion i will provide.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.