Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement the Stack ADT using an Array-based approach. Below I have included all of my completed source and header files. Everything seems to be correct,

Implement the Stack ADT using an Array-based approach. Below I have included all of my completed source and header files. Everything seems to be correct, however I receive errors after running the program. The code compiles, but when I run the program and try to execute any of the functions, they program immediately stops working and closes. Please help me so I can run the program correctly. I believe that the error is somewhere in the StackArray.cpp file because all other files were given to me by an instructor.

//test6.cpp

//--------------------------------------------------------------------

//

// Laboratory 6 test6.cpp

//

// Test program for the operations in the Stack ADT

//

//--------------------------------------------------------------------

#include

using namespace std;

#include "config.h"

#if !LAB6_TEST1

# include "StackArray.cpp"

#else

# include "StackLinked.cpp"

#endif

void print_help()

{

cout << endl << "Commands:" << endl;

cout << " H : Help (displays this message)" << endl;

cout << " +x : Push x" << endl;

cout << " - : Pop" << endl;

cout << " C : Clear" << endl;

cout << " E : Empty stack?" << endl;

cout << " F : Full stack?" << endl;

cout << " Q : Quit the test program" << endl;

cout << endl;

}

template

void test_stack(Stack& testStack)

{

DataType testDataItem; // Stack data item

char cmd; // Input command

print_help();

do

{

testStack.showStructure(); // Output stack

cout << endl << "Command: "; // Read command

cin >> cmd;

if ( cmd == '+' )

cin >> testDataItem;

try {

switch ( cmd )

{

case 'H' : case 'h':

print_help();

break;

case '+' : // push

cout << "Push " << testDataItem << endl;

testStack.push(testDataItem);

break;

case '-' : // pop

cout << "Popped " << testStack.pop() << endl;

break;

case 'C' : case 'c' : // clear

cout << "Clear the stack" << endl;

testStack.clear();

break;

case 'E' : case 'e' : // isEmpty

if ( testStack.isEmpty() )

cout << "Stack is empty" << endl;

else

cout << "Stack is NOT empty" << endl;

break;

case 'F' : case 'f' : // isFull

if ( testStack.isFull() )

cout << "Stack is full" << endl;

else

cout << "Stack is NOT full" << endl;

break;

case 'Q' : case 'q' : // Quit test program

break;

default : // Invalid command

cout << "Inactive or invalid command" << endl;

}

}

catch (logic_error e) {

cout << "Error: " << e.what() << endl;

}

} while ( cin && cmd != 'Q' && cmd != 'q' );

}

int main() {

#if !LAB6_TEST1

cout << "Testing array implementation" << endl;

StackArray s1;

test_stack(s1);

#else

cout << "Testing linked implementation" << endl;

StackLinked s2;

test_stack(s2);

#endif

}

//StackArray.cpp

#include "StackArray.h"

template

StackArray::StackArray(int maxNumber)

{

dataItems = new DataType[maxNumber];

}

template

StackArray::StackArray(const StackArray& other)

{

dataItems = new DataType[maxSize];

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

{

dataItems[i] = other.dataItems[i];

}

}

template

StackArray& StackArray::operator=(const StackArray& other)

{

if (this != &other) return *this;

if (maxSize < other.maxSize) {

delete[] dataItems;

dataItems = new DataType[other.maxSize];

}

maxSize = other.maxSize;

top = other.top;

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

{

dataItems[i] = other.dataItems[i];

}

return *this;

}

template

StackArray::~StackArray()

{

delete[] dataItems;

}

template

void StackArray::push(const DataType& newDataItem) throw (logic_error)

{

if (isFull()) {

throw logic_error("Stack is full.");

}

dataItems[top + 1] = newDataItem;

top++;

}

template

DataType StackArray::pop() throw (logic_error)

{

if (isEmpty()) {

throw logic_error("Stack is empty.");

}

DataType temp = dataItems[top];

dataItems[top] = 0;

top--;

maxSize--;

return dataItems[top + 1];

}

template

void StackArray::clear()

{

top = -1;

}

template

bool StackArray::isEmpty() const

{

if (top == -1){

return true;

}

else{

return false;

}

}

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;

}

//StackLinked.h

//--------------------------------------------------------------------

//

// Laboratory 6 StackArray.h

//

// Class declaration for the array implementation of the Stack ADT

//

//--------------------------------------------------------------------

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include

#include

using namespace std;

#include "Stack.h"

template

class StackLinked : public Stack {

public:

StackLinked(int maxNumber = Stack::MAX_STACK_SIZE);

StackLinked(const StackLinked& other);

StackLinked& operator=(const StackLinked& other);

~StackLinked();

void push(const DataType& newDataItem) throw (logic_error);

DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;

bool isFull() const;

void showStructure() const;

private:

class StackNode {

public:

StackNode(const DataType& nodeData, StackNode* nextPtr)

{

dataItem = nodeData;

next = nextPtr;

}

DataType dataItem;

StackNode* next;

};

StackNode* top;

};

#endif //#ifndef STACKARRAY_H

//StackArray.h

//--------------------------------------------------------------------

//

// Laboratory 6 StackLinked.h

//

// Class declaration for the linked implementation of the Stack ADT

//

//--------------------------------------------------------------------

#ifndef STACKARRAY_H

#define STACKARRAY_H

#include

#include

using namespace std;

#include "Stack.h"

template

class StackArray : public Stack {

public:

StackArray(int maxNumber = Stack::MAX_STACK_SIZE);

StackArray(const StackArray& other);

StackArray& operator=(const StackArray& other);

~StackArray();

void push(const DataType& newDataItem) throw (logic_error);

DataType pop() throw (logic_error);

void clear();

bool isEmpty() const;

bool isFull() const;

void showStructure() const;

private:

int maxSize;

int top;

DataType* dataItems;

};

#endif //#ifndef STACKARRAY_H

//Stack.h

//--------------------------------------------------------------------

//

// Laboratory 6 Stack.h

//

// Class declaration of the abstract class interface to be used as

// the basis for implementations of the Stack ADT.

//

//--------------------------------------------------------------------

// for the exception warnings

#pragma warning( disable : 4290 )

#ifndef STACK_H

#define STACK_H

#include

#include

using namespace std;

template

class Stack {

public:

static const int MAX_STACK_SIZE = 8;

virtual ~Stack();

virtual void push(const DataType& newDataItem) throw (logic_error) = 0;

virtual DataType pop() throw (logic_error) = 0;

virtual void clear() = 0;

virtual bool isEmpty() const = 0;

virtual bool isFull() const = 0;

virtual void showStructure() const = 0;

};

template

Stack::~Stack()

// Not worth having a separate class implementation file for the destuctor

{}

#endif // #ifndef STACK_H

//config.h

/**

* Stack class (Lab 6) configuration file.

* Activate test #N by defining the corresponding LAB6_TESTN to have the value 1.

*/

#define LAB6_TEST1 0 // 0 => use array implementation, 1 => use linked impl.

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

Students also viewed these Databases questions

Question

Does it use a maximum of two typefaces or fonts?

Answered: 1 week ago