Question: Did I do this right? #include StackArray.h template StackArray ::StackArray(int maxNumber) { top = -1; maxSize = maxNumber; dataItems = new DataType[maxSize]; } template StackArray

Did I do this right?

#include "StackArray.h"

template

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

}

}

}

return *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;

}

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!