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

Would someone write detailed comment on following codes please I would really Ap

ID: 3800580 • Letter: W

Question

Would someone write detailed comment on following codes please

I would really Appreciate it

StackArray.cpp

#include "StackArray.h"

template<typename DataType>
inline StackArray<DataType>::StackArray(int maxNumber)
{
   top = -1;
   maxSize = maxNumber;

   dataItems = new DataType[maxSize];
}

template<typename DataType>
StackArray<DataType>::StackArray(const StackArray& other)
{
   clear();

   if (other.top > -1)
   {
       dataItems = new DataType[other.maxSize];
       mazSize = other.maxSize;
       top = other.top;

       for (int i = 0; i <= top; i++)
       {
           dataItems[i] = other.dataItems[i];
       }
   }
}

template<typename DataType>
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{
   if (this != &other)
   {
       clear();

       if (other.top != -1)
       {
           dataItems = new DataType[other.maxSize];
           mazSize = other.maxSize;
           top = other.top;

           for (int i = 0; i <= top; i++)
           {
               dataItems[i] = other.dataItems[i];
           }
       }
   }
   retrun *this;
}

template<typename DataType>
StackArray<DataType>::~StackArray()
{
   clear();
}

template<typename DataType>
void StackArray<DataType>::push(const DataType& newDataItem) throw(logic_error)
{
   if (isFull())
       throw logic_error("The stack is full: No room to add an item. ");

   dataItems[++top] = newDataItem;
}

template<typename DataType>
DataType StackArray<DataType>::pop() throw(logic_error)
{
   if (isEmpty())
       throw logic_error("The stack is empty: No item to remove. ");

   return dataItems[top--];
}

template<typename DataType>
void StackArray<DataType>::clear()
{
   if (dataItems != NULL)
       delete[] dataItems;

   dataItems = NULL;
}

template<typename DataType>
bool StackArray<DataType>::isEmpty() const
{
   return top < 0;
}

template<typename DataType>
bool StackArray<DataType>::isFull() const
{
   return top == (maxSize - 1);
}

template <typename DataType>
void StackArray<DataType>::showStructure() const
// Array implementation. Outputs the data items in a stack. If the
// stack is empty, outputs "Empty stack". This operation is intended
// for testing and debugging purposes only.

{
   if (isEmpty()) {
       cout << "Empty stack." << endl;
   }
   else {
       int j;
       cout << "Top = " << top << endl;
       for (j = 0; j < maxSize; j++)
           cout << j << " ";
       cout << endl;
       for (j = 0; j <= top; j++)
       {
           if (j == top)
           {
               cout << '[' << dataItems[j] << ']' << " "; // Identify top
           }
           else
           {
               cout << dataItems[j] << " ";
           }
       }
       cout << endl;
   }
   cout << endl;
}

template<typename DataType>
void StackArray<DataType>::deleteHelper()
{
   if (dataItems != NULL)
       delete[] dataTiems;

   dataItems = NULL;
}

Stacklinked.cpp

#include "StackLinked.h"

template<typename DataType>
StackLinked<DataType>::StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)
{
   dataItem = nodeData;
   next = nextPtr;
}
template<typename DataType>
StackLinked<DataType>::StackLinked(int maxNumber)
{
   top = NULL;
}

template<typename DataType>
StackLinked<DataType>::StackLinked(const StackLinked& other)
{
   try
   {
       top = NULL;
       if (other.top != NULL)
           copyHelper(this->top, other.top);
   }
   catch (...)
   {
       cout << "Error copying object in copy constructor. ";
       exit(1);
   }
}

template<typename DataType>
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{
   if (this != &other)
   {
       try
       {
           deleteHelper(top);
           if (other->top != NULL)
           {
               stackSizeCounter = other.stackSizeCounter;
               copyHelper(this->top, other->top);
           }
       }
       catch (...)
       {
           cout << "Error deleting/copying object in assignmnet overload method. ";
           exit(1);
       }
   }
   return *this;
}

template<typename DataType>
StackLinked<DataType>::~StackLinked()
{
   if (top != NULL)
   {
       deleteHelper(top);
       top = NULL;
   }
}

template<typename DataType>
void StackLinked<DataType>::push(const DataType& newDataItem) throw(logic_error)
{
   StackNode* temp;

   //if (isFull())
   //   throw logic_error("Stack is Full. ");

   try
   {
       temp = top;
       top = new StackNode(newDataItem, temp);
   }
   catch (...)
   {
       throw logic_error("Error allocating memory in push method. ");
   }
}

template<typename DataType>
DataType StackLinked<DataType>::pop() throw(logic_error)
{
   StackNode* tempNode;
   DataType itemToReturn;

   if (isEmpty())
       throw logic_error("Stack is Empty. ");

   itemToReturn = top->dataItem;
   tempNode = top;
   top = top->next;
   delete tempNode;

   return itemToReturn;
}

template<typename DataType>
void StackLinked<DataType>::clear()
{
   if (top != NULL)
   {
       deleteHelper(top);
       top = NULL;
   }
}

template<typename DataType>
bool StackLinked<DataType>::isEmpty() const
{
   return top == NULL;
}

template<typename DataType>
bool StackLinked<DataType>::isFull() const
{
   return false;
}


template <typename DataType>
void StackLinked<DataType>::showStructure() const

// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs "Empty stack". This operation is
// intended for testing and debugging purposes only.

{
   if (isEmpty())
   {
       cout << "Empty stack" << endl;
   }
   else
   {
       cout << "Top ";
       for (StackNode* temp = top; temp != 0; temp = temp->next) {
           if (temp == top) {
               cout << "[" << temp->dataItem << "] ";
           }
           else {
               cout << temp->dataItem << " ";
           }
       }
       cout << "Bottom" << endl;
   }

}

template<typename DataType>
void StackLinked<DataType>::deleteHelper(StackNode* ptr)
{
   if (ptr->next != NULL)
       deleteHelper(ptr->next);

   delete ptr;
}

template<typename DataType>
void StackLinked<DataType>::copyHelper(StackNode*& ptr, StackNode* otherNode)
{
   ptr = new StackNode(otherNode->dataItem, otherNode->next);

   if (ptr->next != NULL)
       copyHelper(ptr->next, otherNode->next);
}

Again I will really Appreciate it

Thank You

Explanation / Answer

StackArray.cpp

#include "StackArray.h"

//Creates a template named DataType

template<typename DataType>

//Constructor definition StackArray which takes an integer parameter for maximum length
inline StackArray<DataType>::StackArray(int maxNumber)
{

//Initializes the top of stack to -1
top = -1;

//Initializes the data member mazSize of class StackArray to the parameter value maxNumber
maxSize = maxNumber;

//allocates memory using dynamic memory allocation operator new

dataItems = new DataType[maxSize];
}

template<typename DataType>

//Constructor definition takes StackArray object as a reference
StackArray<DataType>::StackArray(const StackArray& other)
{

//Calls the method clear to release memory
clear();

//Checks if top is greater than -1

if (other.top > -1)

{

//Allocates memory
dataItems = new DataType[other.maxSize];

//Sets the data member maxSize to object others maxSize

mazSize = other.maxSize;

//Member top is set to object’s top
top = other.top;

//Loops zero to top value

for (int i = 0; i <= top; i++)
{

/*object’s dataitems i position value is stored in current object’s dataitems i position.*/
dataItems[i] = other.dataItems[i];

}//end of for loop
   }//end of if

}//end of constructor

template<typename DataType>

/* Overloads operator =
returns a object reference of type StackArray

Takes StackArray object as constant reference type*/
StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other)
{

//If the current object is not equal to the parameterized object
if (this != &other)
{

//clear() method called to release memory
clear();

//If parameter’s object top is not equals to -1

if (other.top != -1)
{

//Allocates memory
           dataItems = new DataType[other.maxSize];

//Sets the mazSize objects maxSize
           mazSize = other.maxSize;

//Set the top to object’s top value
           top = other.top;

//Loops from zero to top position

for (int i = 0; i <= top; i++)
{

//Assigns object’s data item to current object’s dataitems
               dataItems[i] = other.dataItems[i];
}//end of for loop

}//end of if
   }//end of if

//Returns the current object
   retrun *this;

}//end of function

template<typename DataType>

//Destructor definition
StackArray<DataType>::~StackArray()
{

//clear() method to release memory
clear();

}//end of destructor

template<typename DataType>

//Function push definition
void StackArray<DataType>::push(const DataType& newDataItem) throw(logic_error)
{

//calls method isFull() to check stack is full or not

// isFull() function returns true if top is equal to maxSize - 1
if (isFull())

//throws an error message
       throw logic_error("The stack is full: No room to add an item. ");

                //If not full increase the top position and stores the value of newDataItem at the top positioin

dataItems[++top] = newDataItem;
}//End of function

template<typename DataType>

//Function pop definition
DataType StackArray<DataType>::pop() throw(logic_error)
{


//calls method isEmpty() to check stack is empty or not

// isEmpty() function returns true if top is equal to - 1

if (isEmpty())

//throws an error message
       throw logic_error("The stack is empty: No item to remove. ");

                //Otherwise returns the top position value and then decrease the top by 1

return dataItems[top--];
}//end of function

template<typename DataType>

//Function clear() definition
void StackArray<DataType>::clear()
{

//If dataItems is not null
if (dataItems != NULL)

//Release the dataItems
       delete[] dataItems;

//Set the dataItems to null

dataItems = NULL;
}//End of clear function

template<typename DataType>

//isEmpty() function definition returns true or false
bool StackArray<DataType>::isEmpty() const
{

//returns true if top is less than zero otherwise returns false
return top < 0;

}//end of function

template<typename DataType>

//isFull() function to return true or false
bool StackArray<DataType>::isFull() const
{

//returns true if top is equal to maxSize – 1 otherwise returns false
return top == (maxSize - 1);

}//end of function

template <typename DataType>
//Displays the structure

void StackArray<DataType>::showStructure() const
// Array implementation. Outputs the data items in a stack. If the
// stack is empty, outputs "Empty stack". This operation is intended
// for testing and debugging purposes only.

{

//Checks whether the stack is empty or not

//isEmpty() function returns true if the top is equals to -1 otherwise false
                if (isEmpty())

{

//If true display message
                                cout << "Empty stack." << endl;
}

//If the isEmpty() returns false
else

{
int j;

//Display the top value

cout << "Top = " << top << endl;
//Loops from zero to maxSize

for (j = 0; j < maxSize; j++)

//Display the value of j i.e., zero to maxSize
           cout << j << " ";

//for new line
       cout << endl;

//Loops from zero to top position
       for (j = 0; j <= top; j++)
       {

//If j value is equals to top
           if (j == top)
           {

//display the dataitems j position value with in square bracket
               cout << '[' << dataItems[j] << ']' << " "; // Identify top
           }

//otherwise display the dataItems j position value
           else
           {
               cout << dataItems[j] << " ";
           }

}
cout << endl;

}
cout << endl;

}//end of function

template<typename DataType>

//Delete data
void StackArray<DataType>::deleteHelper()
{

//if it is not null
   if (dataItems != NULL)

//delete the data
       delete[] dataTiems;

//set the dataItems to null

   dataItems = NULL;
}//end of function

Stacklinked.cpp

#include "StackLinked.h"

template<typename DataType>

/*Constructor for StackNode class which takes nodeData as data item and nextPtr for pointer to next item*/
StackLinked<DataType>::StackNode::StackNode(const DataType& nodeData, StackNode* nextPtr)
{

//assigns paradeter nodeData value to class data member dataItem
dataItem = nodeData;

//next pointer is assigned value of nextPtr to point to next position

next = nextPtr;

}


template<typename DataType>

//Constructor for StackLinked
StackLinked<DataType>::StackLinked(int maxNumber)
{

//Assigns to null
top = NULL;

}

template<typename DataType>

//Constructor for StackLinked which takes StackLinked object as a reference
StackLinked<DataType>::StackLinked(const StackLinked& other)
{

//Begins of try block for exception handlind

try

{

//Initializes current top to null

top = NULL;

//if parameter other object’s top is not null

if (other.top != NULL)

/* call the function copyHelper to create a node and store the data item and point to the next*/

                //passes current object top and parameter object other’s top

copyHelper(this->top, other.top);

}//end of try

//Generic catch which can handle any type exception

catch (...)

{

                //Displays error message

cout << "Error copying object in copy constructor. ";

//exit the program

exit(1);

}//end of catch
}//end of function

template<typename DataType>

//Overloads assignment operator which takes StackLinked class object as a reference
StackLinked<DataType>& StackLinked<DataType>::operator=(const StackLinked& other)
{

                //If current object is not equal to parameter object other

if (this != &other)
{
                //Begin of try block for exception handling

try

{

                //Calls the deleteHelper function and passes the top position

           deleteHelper(top);

                //if parameter object other’s top is not null

           if (other->top != NULL)

           {

                                /* Set the stackSizeCounter to parameter object other’s stackSizseCounter value*/

stackSizeCounter = other.stackSizeCounter;

/* call the function copyHelper to create a node and store the data item and point to the next*/

//passes current top position and parameter object’s top positon
               copyHelper(this->top, other->top);

           }//end of if

}//end of try

//Generic catch which can handle any type exception

catch (...)

{

                //Displays error message
           cout << "Error deleting/copying object in assignmnet overload method. ";

//exit the program

           exit(1);

}//end of catch

}//end of if

//returns the current object

return *this;
}//end of function

template<typename DataType>
//Destructor for StackLinked class

StackLinked<DataType>::~StackLinked()
{

//if top of the stack is not null

if (top != NULL)

{

//call the deleteHelper method to delete to top element
deleteHelper(top);

//set the top to null
top = NULL;

}//end of if
}//end of destructor

template<typename DataType>

//push function for StackLinked class
void StackLinked<DataType>::push(const DataType& newDataItem) throw(logic_error)
{

//Creates a temporary pointer object for StackNode class
StackNode* temp;

   //if (isFull())
   //   throw logic_error("Stack is Full. ");

                //try block begin to handle exception

                try

{

//set the temp pointer to top
       temp = top;

//set the top to new object created by StackNode

       top = new StackNode(newDataItem, temp);

}//end of try

//Generic catch which can handle any type exception

catch (...)

{

                //throws a error message

throw logic_error("Error allocating memory in push method. ");

}//end of catch

}//end of function

template<typename DataType>

//pop function
DataType StackLinked<DataType>::pop() throw(logic_error)
{

//Creates a temporary object pointer of StackNode class

StackNode* tempNode;

//Creates an object of DataType to return an item

DataType itemToReturn;

//checks whether the stack is empty or not by calling isEmpty() function

//isEmpty() function return true if the stack is empty otherwise false

if (isEmpty())

                //throws an error message

throw logic_error("Stack is Empty. ");

                //if isEmpty() is false

                //set the itemToReturn to the dataItem at the top position

itemToReturn = top->dataItem;

//set the tempNode pointer point to top position

tempNode = top;

//Set the top to top next position

top = top->next;

//Delete the temporary pointer

delete tempNode;

//return the itemToReturn

return itemToReturn;
}//End of function

template<typename DataType>

//clear() function to release memory
void StackLinked<DataType>::clear()
{

//if top is not null

if (top != NULL)

{

                //call the deleteHelper function to delete the top position

deleteHelper(top);

//set the top to null

top = NULL;

}//end of if

}//end of function

template<typename DataType>

//isEmpty() function returns true if the top is null otherwise false
bool StackLinked<DataType>::isEmpty() const
{

//checks top is null or not

return top == NULL;
}//end of function

template<typename DataType>
//returns false

bool StackLinked<DataType>::isFull() const
{
   return false;
}


template <typename DataType>

//Displays the structure
void StackLinked<DataType>::showStructure() const

// Linked list implementation. Outputs the data elements in a stack.
// If the stack is empty, outputs "Empty stack". This operation is
// intended for testing and debugging purposes only.

{

                //checks whether the stack is empty or not

if (isEmpty())

{

                //displays message

cout << "Empty stack" << endl;

}

else

{

                //displays the message top

cout << "Top ";

//loops from top position till zero

for (StackNode* temp = top; temp != 0; temp = temp->next)

{

                //checks if the temporary pointer temp is equal to top

if (temp == top)

{

                /*displays dataItem pointed by temporary object temp within square bracket*/

cout << "[" << temp->dataItem << "] ";

}//end of if

else

{

                //displays the dataItem pointed by the temporary temp pointer

cout << temp->dataItem << " ";

}//end of else

}//end of for loop

//displays message Bottom

cout << "Bottom" << endl;
   }//end of else

}//end of function

template<typename DataType>

//function to delete the node given to it as a parameter
void StackLinked<DataType>::deleteHelper(StackNode* ptr)
{

//checks current pointer next is not null

if (ptr->next != NULL)

                //calls the deleteHelper function to delete the node

       deleteHelper(ptr->next);

                delete ptr;
}//end of function

template<typename DataType>

//function to create a node and store data
void StackLinked<DataType>::copyHelper(StackNode*& ptr, StackNode* otherNode)
{

//Creates a node by calling the constructor passing the dataItem and next pointer position

ptr = new StackNode(otherNode->dataItem, otherNode->next);

//checks if the current next is not null the call the copyHelper to store the data

if (ptr->next != NULL)

       copyHelper(ptr->next, otherNode->next);
}//end of function

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