Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions