In: Computer Science
C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray class
Both given below:
5.1 Implement QueueArray<DataType>::QueueArray(int maxNumber)
5.2 Implement QueueArray<DataType>::QueueArray(const QueueArray& other)
5.3 Implement QueueArray<DataType>::~QueueArray()
5.4 Implement void
QueueArray<DataType>::enqueue(const DataType&
newDataItem) throw (logic_error)
5.5 Implement DataType QueueArray<DataType>::dequeue() throw (logic_error)
5.6 Implement void void QueueArray<DataType>::clear() throw (logic_error)
5.7 Implement bool QueueArray<DataType>::isEmpty() const
5.8 Implement bool QueueArray<DataType>::isFull() const
5.9 Implement void
QueueArray<DataType>::putFront(const DataType&
newDataItem) throw (logic_error)
5.10 Implement DataType QueueArray<DataType>::getRear() throw (logic_error)
5.11 Implement int QueueArray<DataType>::getLength() const
#include "QueueArray.h"
template <typename DataType>
QueueArray<DataType>::QueueArray(int maxNumber)
{
}
template <typename DataType>
QueueArray<DataType>::QueueArray(const QueueArray&
other)
{
}
template <typename DataType>
QueueArray<DataType>&
QueueArray<DataType>::operator=(const QueueArray&
other)
{
}
template <typename DataType>
QueueArray<DataType>::~QueueArray()
{
}
template <typename DataType>
void QueueArray<DataType>::enqueue(const DataType&
newDataItem) throw (logic_error)
{
}
template <typename DataType>
DataType QueueArray<DataType>::dequeue() throw
(logic_error)
{
DataType temp;
return temp;
}
template <typename DataType>
void QueueArray<DataType>::clear()
{
}
template <typename DataType>
bool QueueArray<DataType>::isEmpty() const
{
return false;
}
template <typename DataType>
bool QueueArray<DataType>::isFull() const
{
return false;
}
template <typename DataType>
void QueueArray<DataType>::putFront(const DataType&
newDataItem) throw (logic_error)
{
}
template <typename DataType>
DataType QueueArray<DataType>::getRear() throw
(logic_error)
{
DataType temp;
return temp;
}
template <typename DataType>
int QueueArray<DataType>::getLength() const
{
return -1;
}
//--------------------------------------------------------------------
template <typename DataType>
void QueueArray<DataType>::showStructure() const
// Array implementation. Outputs the data items in a queue. If
the
// queue is empty, outputs "Empty queue". This operation is
intended
// for testing and debugging purposes only.
{
int j; // Loop counter
if ( front == -1 )
cout << "Empty queue" << endl;
else
{
cout << "Front = " << front << " Back = "
<< back << endl;
for ( j = 0 ; j < maxSize ; j++ )
cout << j << "\t";
cout << endl;
if ( back >= front )
for ( j = 0 ; j < maxSize ; j++ )
if ( ( j >= front ) && ( j <= back ) )
cout << dataItems[j] << "\t";
else
cout << " \t";
else
for ( j = 0 ; j < maxSize ; j++ )
if ( ( j >= front ) || ( j <= back ) )
cout << dataItems[j] << "\t";
else
cout << " \t";
cout << endl;
}
}
copyable code:
individual code implementation listed
5.1)
QueueArray<DataType>::QueueArray(int maxNumber)
{
Size_max = maxNumber;
//initidates begin and rear at same position at the begin
begin = -1;
rear = -1;
dataset_val= NULL;
//dynamically allocates memory
dataset_val = new DataType [maxNumber];
}
5.2)
template <typename DataType>
QueueArray<DataType>::QueueArray(const QueueArray& other)
{
Size_max = NULL;
begin = -1;
rear = -1;
dataset_val = NULL;
//overloading operator to avoid replication
*this = other;
}
template <typename DataType>
QueueArray<DataType>& QueueArray<DataType>::operator=(const QueueArray<DataType>& other)
{
int ptr = -1;
// validates *this to check it copies itself
if( this == &other ) {
return *this;
}
else {
// copies
if( Size_max != other.Size_max ) {
// clone Size_max variable
Size_max = other.Size_max;
if( dataset_val != NULL ) {
// dynamic memeory value returned
delete [] dataset_val;
}
// allocate new memry
dataset_val = new DataType [Size_max];
}
// clone the front and rear ptrs
begin = other.begin;
rear = other.rear;
ptr = begin;
if( (begin == rear) && (begin > -1) ) {
dataset_val[ptr] = other.dataset_val[ptr];
}
else if( begin > -1 ) {
do {
if( ptr == Size_max ) {
ptr = 0;
}
dataset_val[ptr] = other.dataset_val[ptr];
++ ptr;
} while( ptr != (rear + 1) );
}
}
return *this;
}
5.3)
template <typename DataType>
QueueArray<DataType>::~QueueArray()
{
delete [] dataset_val;
dataset_val = NULL;
}
5.4)
template <typename DataType>
void QueueArray<DataType>::enqueue(const DataType& newDataItem)
throw (logic_error)
{
if( isFull() ) {
cout << "Error: Queue full!!!." << endl;
}
else {
if( isEmpty() ) {
begin = 0;
rear = 0;
}
else {
++ rear;
if( rear == Size_max ) {
rear = 0;
}
}
dataset_val[rear] = newDataItem;
}
}
5.5)
template <typename DataType>
DataType QueueArray<DataType>::dequeue() throw (logic_error){
int return_ptr = begin;
if( isEmpty() ) {
cout << "Error: Empty queue." << endl;
}
else {
++ begin;
if( begin == Size_max ) {
begin = 0;
}
if( (begin == (rear + 1))|| ((begin == 0) && (rear == (Size_max - 1))) ) {
begin = -1;
rear = -1;
}
return dataset_val[return_ptr];
}
}
5.6)
template <typename DataType>
void QueueArray<DataType>::clear() {
begin = -1;
rear = -1;
}
5.7)
template <typename DataType>
bool QueueArray<DataType>::isEmpty() const {
return (begin == -1);
}
5.8)
template <typename DataType>
bool QueueArray<DataType>::isFull() const
{
if( (begin == 0) && (rear == (Size_max - 1)) ) {
return true;
}
else if( rear == (begin - 1) ) {
return true;
}
else {
return false;
}
}
5.9)
template <typename DataType>
void QueueArray<DataType>::putFront(const DataType& newDataItem)
throw (logic_error) {
if( isFull() ) {
cout << "Error: queue is full." << endl;
}
else {
if( isEmpty() ) {
begin = 0;
rear = 0;
}
else {
-- begin;
if( begin < 0 ) {
begin = (Size_max - 1);
}
}
dataset_val[begin] = newDataItem;
}
}
5.10)
template <typename DataType>
DataType QueueArray<DataType>::getRear() throw (logic_error) {
int return_ptr = -1;
if( isEmpty () ) {
cout << "Error: queue empty." << endl;
}
else {
return_ptr = rear;
if( begin == rear ) {
begin = -1;
rear = -1;
}
else {
-- rear;
if( rear < 0 ) {
rear = (Size_max - 1);
}
}
return dataset_val[return_ptr];
}
}
5.11)
template <typename DataType>
int QueueArray<DataType>::getLength() const
{
int len = -8;
int inter_med = 0;
if( begin < rear ) {
len = (rear - begin + 1);
}
else if( rear < begin ) {
inter_med = (Size_max - begin);
inter_med += rear;
len = (inter_med + 1);
}
else if( (begin == rear) && (begin >= 0) ) {
len = 1;
}
else {
len = 0;
}
return len;
}
//For the above code the show structure template will be
template <typename DataType>
void QueueArray<DataType>::showStructure() const
{
int j;
if ( begin == -1 )
cout << "Empty queue" << endl;
else
{
cout << "begin = " << begin << " rear = " << rear << endl;
for ( j = 0 ; j < Size_max ; j++ )
cout << j << "\t";
cout << endl;
if ( rear >= begin )
for ( j = 0 ; j < Size_max ; j++ )
if ( ( j >= begin ) && ( j <= rear ) )
cout << dataset_val[j] << "\t";
else
cout << " \t";
else
for ( j = 0 ; j < Size_max ; j++ )
if ( ( j >= begin ) || ( j <= rear ) )
cout << dataset_val[j] << "\t";
else
cout << " \t";
cout << endl;
}
}