In: Computer Science
Having Trouble with this C++ assignment THe places that are marked // TODO is where code should be filled in at
Header (myQueue.h)
#ifndef _MYQUEUE_H_
#define _MYQUEUE_H_
using namespace std;
template
class myQueue {
public:
myQueue(int maxSz);
~myQueue();
void enqueue(T item);
T dequeue();
int currentSize();
bool isEmpty();
bool isFull();
private:
T *contents; /*Dynamic initiate (C++ keyword new) the
holder array*/
int front,rear; /*Index in the array of the front and
rear element*/
int arrayLength; /*The length of the contents holder
array*/
/* Keep in mind that the Queue will
only hold up to (arrayLength - 1) elements*/
};
template
myQueue::myQueue(int maxSz) {
// TODO
}
template
myQueue::~myQueue() {
// TODO
}
template
void myQueue::enqueue(T item) {
// TODO
}
template
T myQueue::dequeue() {
// TODO
}
template
int myQueue::currentSize() {
// TODO
}
template
bool myQueue::isEmpty() {
// TODO
}
template
bool myQueue::isFull() {
// TODO
}
#endif
Queue Test (queueTest.cpp)
#include
#include "myQueue.h"
using namespace std;
int main() {
cout << "Testing the template myQueue, try an
integer queue as an example..." << endl;
cout << "Please enter the max size of the int
queue: ";
int capacity;
cin >> capacity;
myQueue testIntQ(capacity);
while(1) {
cout << "Please enter 'e' for
enqueue, 'd' for dequeue, and 's' for stop." << endl;
char userOption;
cin >> userOption;
if(userOption == 's')
break;
switch(userOption) {
case 'e':
if(!testIntQ.isFull()) {
cout << "Please enter
the integer you want to enqueue: ";
int val;
cin >> val;
testIntQ.enqueue(val);
}
else
cout << "Cannot
enqueue. The queue is full." << endl;
break;
case 'd':
if(!testIntQ.isEmpty())
cout <<
testIntQ.dequeue() << " has been popped out." <<
endl;
else
cout << "Cannot pop.
The queue is empty." << endl;
break;
default:
cout << "Illegal input character for
options." << endl;
}
}
return 0;
}
(40’) In myQueue.h, implement the queue class template, myQueue. Keep in mind, the arrayLength needs to be one more than the capacity of the queue. Also, under this implementation, make sure your calculation of currentSize is correct, and the conditions for “Full” and “Empty” are correct. One shortcut could be: once you make sure currentSize() is implemented correctly, you might use it in isFull() and isEmpty(), and the number of elements in the queue must range from 0 to arrayLength – 1.
// myQueue.h
#ifndef _MYQUEUE_H_
#define _MYQUEUE_H_
template <class T>
class myQueue {
public:
myQueue(int maxSz);
~myQueue();
void enqueue(T item);
T dequeue();
int currentSize();
bool isEmpty();
bool isFull();
private:
T *contents; /*Dynamic initiate (C++ keyword new) the holder array*/
int front,rear; /*Index in the array of the front and rear element*/
int arrayLength; /*The length of the contents holder array*/
/* Keep in mind that the Queue will only hold up to (arrayLength - 1) elements*/
};
// constructor to create a queue of size maxSz
template <class T>
myQueue<T>::myQueue(int maxSz) {
arrayLength = maxSz;
contents = new T[arrayLength];
front = -1;
rear = -1;
}
// destructor to deallocate the memory
template <class T>
myQueue<T>::~myQueue() {
if(contents != NULL)
delete [] contents;
}
// function to add the item at the end of queue
template <class T>
void myQueue<T>::enqueue(T item) {
if(!isFull()) // check if queue is full
{
if(front == -1) // if queue is empty, insert it as the first element
{
front++;
rear++;
contents[front]=item;
}else /// else add the item at the end of queue
{
rear++;
contents[rear] = item;
}
}
}
// function to delete and return the front element of the queue
template <class T>
T myQueue<T>::dequeue() {
if(!isEmpty()) // check if queue is empty
{
T item = contents[front]; // get the front element
front++;
// if queue is empty, make front and rear = -1
if(front > rear)
{
rear = -1;
front = -1;
}else
{
// move the elements to the left
for(int i=front-1;i<rear;i++)
contents[i] = contents[i+1];
front--;
rear--;
}
return item; // return the item
}
return T(NULL);
}
// function to return the current size of the queue
template <class T>
int myQueue<T>::currentSize() {
return (rear+1);
}
// function to return if the queue is empty or not
template <class T>
bool myQueue<T>::isEmpty() {
return(front == -1);
}
// function to return if queue is full or not
template <class T>
bool myQueue<T>::isFull() {
return(rear == (arrayLength-1));
}
#endif
//end of myQueue.h
//queueTest.cpp : C++ program to test the MyQueue class
#include <iostream>
#include "myQueue.h"
using namespace std;
int main() {
cout << "Testing the template myQueue, try an integer queue as an example..." << endl;
cout << "Please enter the max size of the int queue: ";
int capacity;
cin >> capacity;
myQueue<int> testIntQ(capacity);
while(1) {
cout << "Please enter 'e' for enqueue, 'd' for dequeue, and 's' for stop." << endl;
char userOption;
cin >> userOption;
if(userOption == 's')
break;
switch(userOption) {
case 'e':
if(!testIntQ.isFull()) {
cout << "Please enter the integer you want to enqueue: ";
int val;
cin >> val;
testIntQ.enqueue(val);
}
else
cout << "Cannot enqueue. The queue is full." << endl;
break;
case 'd':
if(!testIntQ.isEmpty())
cout << testIntQ.dequeue() << " has been popped out." << endl;
else
cout << "Cannot pop. The queue is empty." << endl;
break;
default:
cout << "Illegal input character for options." << endl;
}
}
return 0;
}
//end of queueTest.cpp
Output: