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
{
top = -1;
maxSize = maxNumber;
dataItems = new DataType[maxSize];
}
template
StackArray
{
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
{
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
{
clear();
}
template
void StackArray
{
if (isFull())
throw logic_error("The stack is full: No room to add an item. ");
dataItems[++top] = newDataItem;
}
template
DataType StackArray
{
if (isEmpty())
throw logic_error("The stack is empty: No item to remove. ");
return dataItems[top--];
}
template
void StackArray
{
if (dataItems != NULL)
delete[] dataItems;
dataItems = NULL;
}
template
bool StackArray
{
return top < 0;
}
template
bool StackArray
{
return top == (maxSize - 1);
}
template
void StackArray
// 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started