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

C++ Project Create a templated queue class with an array based variant. An examp

ID: 3827306 • Letter: C

Question

C++ Project

Create a templated queue class with an array based variant.

An example header file below is given holding elements of type class DataType. This class should be templated

const int ARRAY_MAX = 1000;

class ArrayQueue{

public:

ArrayQueue(); //Instantiate a new Queue object with no valid data

ArrayQueue(int size, const DataType& value); // Instantiate a new Queue object which will hold int size number of elements in total, all of them initialized to be equal to parameter //value

ArrayQueue(const ArrayQueue& other); //Instantiate a new Queue object which will be a seperate copy of the data of the other Queue object which is getting copied

~ArrayQueue(); // Destroys the instance of the Queue object

ArrayQueue& operator=(const ArrayQueue& other_arrayQueue); // Will assign a new value to the calling Queue object, which will be an exact copy of the other_arrayQueue object //passed as a parameter. Returns a reference to the calling object to be used for cascading operator=

DataType& front(); //returns a reference to the front element of the queue (before calling this method ensure the queue isnt empty)

const DataType& front() const;

DataType& back(); //returns a reference to the back element of the queue (before calling this method ensure the queue isnt empty)

const DataType& back() const;

void push(const DataType& value); // Inserts at the back of the Queue an element of the given value

void pop(); //removes from the front element of the queue

int size() const; // returns the size of the current queue

bool empty() const; // will return true if the queue is empty

bool full() const; // will return true if the queue is full

void clear(); // after called the queue will be considered empty

friend std::ostream& operator<<(std::ostream& os, const ArrayQueue& arrayQueue); //will output the complete content of the calling queue object. (Not required to be templated)

private:

DataType m_array[ARRAY_MAX]; //The array that holds the data. Both parameters determined via template parameters

int m_front; // an int with the respective m_array index of the front element of the queue

int m_back; // an int with the respective m_array index of the last element of the queue

int m_size; // keeps track of how many elements are in the queue (should not exceed ARRAY_MAX)

};

/*The implementation will be a wrap around queue, meaning that,

a) pushing an element will move the back by one (m_back=(m_back+1)%ARRAY_MAXSIZE) //and increase the size by one

b) popping an element will move the front by one (m_front=(m_front+1)%ARRAY_MAXSIZE) // and decrease size by one

*/

Explanation / Answer

#include<iostream>
#include<stdlib.h>
#include<process.h>
using namespace std;
const int ARRAY_MAX = 1000;
template<class T>
class ArrayQueue
{
private:
int m_front,m_back;
           int size;
T *queue;
public:
ArrayQueue(int maxqueuesize)
{
m_front=0;
m_back=-1;
                   size = 0;
queue=new T[ARRAY_MAX];
}
           ArrayQueue(int maxqueuesize,T& value)
{
m_front=0;
m_back=-1;
                   size = 0;
queue=new T[maxqueuesize];
                   for(int i=0; i<maxqueuesize; i++){
                       queue[i]=value;
                   }
}
           ArrayQueue(const ArrayQueue& other)
{
m_front=0;
m_back=-1;
                   size = 0;
queue=new T[other.size()];
                   for(int i=0; i<other.size(); i++){
                       T node = new T(other[i]);
                       queue[i]=node;
                   }
}

           ArrayQueue& operator=(const ArrayQueue& other_arrayQueue);
~ArrayQueue()
{
delete[] queue;
}
bool isempty();
bool isfull();
           int getsize();
void enqueue(T ele);
T dequeue();
T front();
T back();
void display();
};

template<class T>
bool ArrayQueue<T>::isempty()
{
if(m_front==0&&m_back==-1||m_front==m_back)
return true;
else
return false;
}

template<class T>
bool ArrayQueue<T>::isfull()
{
if(m_back==ARRAY_MAX-1)
return true;
else
return false;
}

template<class T>
T ArrayQueue<T>::front()
{
if(isempty())
       return NULL;
else
       return queue[m_front];
}

template<class T>
T ArrayQueue<T>::back()
{
if(isempty())
       return NULL;
else
       return queue[m_back];
}
template<class T>

int ArrayQueue<T>::getsize(){
   return m_back;
}  

template<class T>
void ArrayQueue<T>::enqueue(T ele)
{
if(isfull())
cout<<" Sorry the queue is full!";
else
{
cout<<" Enter the element to insert : ";
cin>>ele;
queue[++m_back]=ele;
}
}

template<class T>
T ArrayQueue<T>::dequeue()
{
if(isempty())
       return NULL;
else
       return queue[m_front++];
}

template<class T>
void ArrayQueue<T>::display()
{
if(isempty())
cout<<" Sorry the queue is empty!";
else
{
cout<<" Queue elements are : ";
for(int i=m_front;i<=m_back;i++)
{
cout<<" "<<queue[i];
}
}
}

int main(){

   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