Question
C++ Modify the Queue class to include appropriate error messages if invalid conditions occurfor example, trying to dequeue an item when the queue is empty.
C++ Modify the Queue class to include appropriate error messages if invalid conditions occurfor example, trying to dequeue an item when the queue is empty.
A SIMPLE QUEUE CLASS
main.cpp
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
#include "Queue.h"
#include
using namespace std;
int main()
{
Queue q;
cout << "Insertion of 10 characters in q" << endl;
for(int i=0; i < q.getSize(); i++)
{
char x = 32 + rand()%95;
cout << x << endl;
q.enqueue(x);
}
cout << endl
<< "Displaying and deleting elements from q" << endl;
while(!q.isEmpty())
{
cout << "Item at the front: " << q.getFront() << endl;
q.dequeue();
}
return 0;
}
Queue.cpp
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
#include "Queue.h"
/*
* Constructor. Initializes the queue.
*/
Queue::Queue()
{
front = 0;
back = SIZE-1;
count = 0;
}
/*
* Determines whether the queue is empty.
*
* Returns true if the queue is empty, false otherwise.
*/
bool Queue::isEmpty()
{
return count == 0;
}
/*
* Adds an element to the back of the queue.
*
* x: element to be added to the queue.
*/
void Queue::enqueue(char x)
{
back = (back+1)%SIZE;
list[back] = x;
count++;
}
/*
* Removes the element in the front of the queue.
*/
void Queue::dequeue()
{
front = (front+1)%SIZE;
count--;
}
/*
* Returns the element in the front of the queue. Does not remove it.
*/
char Queue::getFront()
{
return list[front];
}
/*
* Returns the size of the queue.
*/
int Queue::getSize()
{
return SIZE;
}
Queue.h
/*******************************
* Week 2 lesson: *
* a simple Queue class *
*******************************/
/*
* Class implementing a Queue ADT.
*/
class Queue
{
public:
Queue();
bool isEmpty();
void enqueue(char);
void dequeue();
char getFront();
int getSize();
private:
static const int SIZE = 10; //size of the queue array
char list[SIZE]; //array to store the queue items
int count; //number of items in the queue
int front, back; //front and back locations
};
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started