Question: Would someone write detailed comment on following codes please I would really Appreciate it StackArray.cpp #include StackArray.h template inline StackArray ::StackArray(int maxNumber) { top =

Would someone write detailed comment on following codes please

I would really Appreciate it

StackArray.cpp

#include "StackArray.h"

template inline StackArray::StackArray(int maxNumber) { top = -1; maxSize = maxNumber;

dataItems = new DataType[maxSize]; }

template StackArray::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 StackArray& StackArray::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 StackArray::~StackArray() { clear(); }

template void StackArray::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 DataType StackArray::pop() throw(logic_error) { if (isEmpty()) throw logic_error("The stack is empty: No item to remove. ");

return dataItems[top--]; }

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

dataItems = NULL; }

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

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

template void StackArray::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 << "\t"; cout << endl; for (j = 0; j <= top; j++) { if (j == top) { cout << '[' << dataItems[j] << ']' << "\t"; // Identify top } else { cout << dataItems[j] << "\t"; } } cout << endl; } cout << endl; }

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

dataItems = NULL; }

Stacklinked.cpp

#include "StackLinked.h"

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

template StackLinked::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 StackLinked& StackLinked::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 StackLinked::~StackLinked() { if (top != NULL) { deleteHelper(top); top = NULL; } }

template void StackLinked::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 DataType StackLinked::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 void StackLinked::clear() { if (top != NULL) { deleteHelper(top); top = NULL; } }

template bool StackLinked::isEmpty() const { return top == NULL; }

template bool StackLinked::isFull() const { return false; }

template void StackLinked::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\t"; for (StackNode* temp = top; temp != 0; temp = temp->next) { if (temp == top) { cout << "[" << temp->dataItem << "]\t"; } else { cout << temp->dataItem << "\t"; } } cout << "Bottom" << endl; }

}

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

delete ptr; }

template void StackLinked::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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!