Question: I need help with creating code for 'stack by means of an array' I have created the header files to start the cpp file. ArrayStack.h:

I need help with creating code for 'stack by means of an array'

I have created the header files to start the cpp file.

ArrayStack.h:

#include

#include "RuntimeException.h"

#include "StackException.h"

#ifndef ArrayStack_h

#define ArrayStack_h

template

class ArrayStack {

enum { DEF_CAPACITY = 100 }; // default stack capacity

public:

ArrayStack(int cap = DEF_CAPACITY); // constructor from capacity

int size() const; // number of items in the stack

bool empty() const; // is the stack empty?

const E& top() const throw(StackEmpty); // get the top element

void push(const E& e) throw(StackFull); // push element onto stack

void pop() throw(StackEmpty); // pop the stack

// ...housekeeping functions omitted

private: // member data

E* S; // array of stack elements

int capacity; // stack capacity

int t; // index of the top of the stack

};

template ArrayStack::ArrayStack(int cap)

: S(new E[cap]), capacity(cap), t(-1) { } // constructor from capacity

template int ArrayStack::size() const

{ return (t + 1); } // number of items in the stack

template bool ArrayStack::empty() const

{ return (t < 0); } // is the stack empty?

template // return top of stack

const E& ArrayStack::top() const throw(StackEmpty) {

if (empty()) throw StackEmpty("Top of empty stack");

return S[t];

}

template // push element onto the stack

void ArrayStack::push(const E& e) throw(StackFull) {

if (size() == capacity) throw StackFull("Push to full stack");

S[++t] = e;

}

template // pop the stack

void ArrayStack::pop() throw(StackEmpty) {

if (empty()) throw StackEmpty("Pop from empty stack");

--t;

}

#endif /* ArrayStack_h */

StackException.h:

#ifndef StackException_h_

#define StackException_h_

#include "RuntimeException.h"

// Exception thrown on performing top or pop of an empty stack.

class StackEmpty : public RuntimeException {

public:

StackEmpty(const std::string& err) : RuntimeException(err) {}

};

class StackFull : public RuntimeException {

public:

StackFull(const std::string& err) : RuntimeException(err) {}

};

#endif /* StackException_h */

RuntimeException.h:

#ifndef RUNTIMEEXCEPTION_H

#define RUNTIMEEXCEPTION_H

#include

class RuntimeException {

// generic run-time exception private:

std::string errorMsg;

public:

RuntimeException(const std::string& err) { errorMsg = err; }

std::string getMessage() const { return errorMsg; }

};

#endif

I have started the "stack.cpp" with followings:

#include

#include

#include "ArrayStack.h"

#include "LinkedStack.h"

#include "StackException.h"

using namespace std;

int main()

{

return EXIT_SUCCESS;

}

In the main function, I need to create a code for the following:

1) Modify the main() in stack.cpp to declare an ArrayStack of integer type. Execute the interface commands below in order:

a. push(7)

b. push(13);

c. print the top elements

d. pop()

e. push(9)

f. print the top element

g. print the top element

h. pop();

Anyone would like to help me out here? 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!