Question

In: Computer Science

C++ Directions: Follow the insttructions of implementation and then fill in the following code for QueueArray...

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;
}
}

Solutions

Expert Solution

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;

    }

}


Related Solutions

Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input...
Using the following code perform ALL of the tasks below in C++: ------------------------------------------------------------------------------------------------------------------------------------------- Implementation: Overload input operator>> a bigint in the following manner: Read in any number of digits [0-9] until a semi colon ";" is encountered. The number may span over multiple lines. You can assume the input is valid. Overload the operator+ so that it adds two bigint together. Overload the subscript operator[]. It should return the i-th digit, where i is the 10^i position. So the first...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
c++ I need a code that will fill an array size of 1000, an array of...
c++ I need a code that will fill an array size of 1000, an array of size 2000, and an array size of 10000, with random int values. Basically like this: array1[1000] = filled all with random numbers array2[2000] = filled all with random numbers array3[10000] = filled all with random numbers C++ no need for print
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit...
Directions: Convert the following problems below to c++ equivalent code Problem 3: Take any 2 digit number from 31 to 99. (You do not have to check the numbers just assume the user will enter a valid number so long as you tell them to). Store an extra copy of this number for later 3.   Add 82 to the number. 4.    Remove the hundreds place from your number 5.    Add 1 to the number. 6.    Subtract this from your...
For the following items, follow the directions, write the correct answer in the blank, or circle...
For the following items, follow the directions, write the correct answer in the blank, or circle the correct answer. Having applied for a job at the Commerce Department’s Bureau of Economic Analysis, you are given the following hypothetical data to study before your interview.  Figures are total value in billions of dollars. Household spending on:             Services                                                =          $3,008             Nondurable goods                                =          $1,776             Durable goods                                      =            $706 Business spending on plant and equipment        =             $815 Wear and tear on factory equipment                 =            $200 Inventory changes                                             =                $9 New homes constructed                                    =           $397 Existing homes which were...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that...
The following code fragment is expressed in arm assembly code. Fill in the blanks, so that it is equivalent to the following C code. int counter; int x = 5; int y = 6; for (counter =10; counter >0;counter--) IF(X==Y) Y = Y + 1 ; ELSE Y = Y + 2} Fill in the blanks in the following code: MOV__________ ;loop counter into r0-ten times round the loop MOV__________ ;Value of y loaded into r1 MOV__________ ;Value of x...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the...
Given the following code for AES Electronic Code Block implementation for the encryption functionality. Modify the code to create a function named ‘encryptECB(key, secret_message)’ that accepts key and secret_message and returns a ciphertext.          #Electronic Code Block AES algorithm, Encryption Implementation from base64 import b64encode from Crypto.Cipher import AES from Crypto.Util.Padding import pad from Crypto.Random import get_random_bytes secret_message = b" Please send me the fake passport..." password = input ("Enter password to encrypt your message: ") key= pad(password.encode(), 16) cipher...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT