Question: Write an array - based implementation of a queue that uses a resizable, circular array to represent the items in the queue. Start with the
Write an arraybased implementation of a queue that uses a resizable, circular array to represent the items in the queue. Start with the files that I am linking to below. Your class should have a DEFAULTCAPACITY constant and also a capacity data member. For submission purposes, set the DEFAULTCAPACITY to Your class should double the size of the array when an attempt is made to enqueue an item when the capacity is full. After the first doubling of the array, halve the size of the queue when a pop results in fewer than half of the array's locations being occupied by current queue items. You will need to change the push and pop functions to void instead of bool. push will be void because it is no longer possible to exceed the queue's capacity. pop will be void because you should throw a PrecondViolatedExcep exception if the queue is empty, instead of returning false. You'll also need to make these two small changes in the QueueInterface.h file.
QueueInterface.h:
#ifndef QUEUEINTERFACE
#define QUEUEINTERFACE
template
class QueueInterface
public:
virtual bool empty const ;
virtual bool pushconst ItemType& newEntry;
virtual ~QueueInterface
;
#endif
ArrayQueuecpp:
#include
template
ArrayQueue::ArrayQueue
qfront ;
back DEFAULTCAPACITY ;
numItems ;
template
bool ArrayQueue::empty const
return numItems ;
template
bool ArrayQueue::pushconst ItemType& newEntry
bool result false;
if numItems DEFAULTCAPACITY
back back DEFAULTCAPACITY;
itemsback newEntry;
numItems;
result true;
return result;
template
bool ArrayQueue::pop
bool result false;
if empty
qfront qfront DEFAULTCAPACITY;
numItems;
result true;
return result;
template
ItemType ArrayQueue::front const
if empty
throw PrecondViolatedExceppeekFront called with empty queue";
return itemsqfront;
template
void ArrayQueue::print const
std::cout "Here is the queue: ;
if empty
std::cout "empty";
else
for int i qfront; i back; i i DEFAULTCAPACITY
std::cout itemsi;
std::cout itemsback;
ArrayQueueh:
#ifndef ARRAYQUEUEH
#define ARRAYQUEUEH
#include "QueueInterface.h
#include "PrecondViolatedExcep.h
template
class ArrayQueue : public QueueInterface
public:
ArrayQueue;
bool empty const;
bool pushconst ItemType& newEntry;
bool pop;
ItemType front const;
void print const;
private:
static const int DEFAULTCAPACITY ;
ItemType itemsDEFAULTCAPACITY;
int qfront;
int back;
int numItems;
;
#include "ArrayQueue.cpp
#endif
PrecondViolatedExcepcpp:
#include "PrecondViolatedExcep.h
PrecondViolatedExcep::PrecondViolatedExcepconst std::string& message
: std::logicerrorPrecondition Violated Exception: message
PrecondViolatedExcep.h:
#ifndef PRECONDVIOLATEDEXCEPH
#define PRECONDVIOLATEDEXCEPH
#include
#include
class PrecondViolatedExcep : public std::logicerror
public:
PrecondViolatedExcepconst std::string& message ;
;
#include "PrecondViolatedExcep.cpp
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
