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

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 <iostream>

#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 <string>

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 <iostream>

#include <string>

#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   

Solution

// There were some issue with template in your code which I have fixed

here are four files

// ArrayStack.h

#include <iostream>
#include \"RuntimeException.h\"
#include \"StackException.h\"
#ifndef ArrayStack_h
#define ArrayStack_h
template <class E>
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 <class E>
ArrayStack<E>::ArrayStack(int cap) : S(new E[cap]), capacity(cap), t(-1) { } // constructor from capacity

template <class E>
int ArrayStack<E>::size() const
{ return (t + 1); } // number of items in the stack

template <class E>
bool ArrayStack<E>::empty() const
{ return (t < 0); } // is the stack empty?

template <class E> // return top of stack
const E& ArrayStack<E>::top() const throw(StackEmpty) {
if (empty()) throw StackEmpty(\"Top of empty stack\");
return S[t];
}

template <class E> // push element onto the stack
void ArrayStack<E>::push(const E& e) throw(StackFull) {
if (size() == capacity) throw StackFull(\"Push to full stack\");
S[++t] = e;
}
template <class E> // pop the stack
void ArrayStack<E>::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 <string>
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

// stack.cpp

#include <iostream>
#include <string>
#include \"ArrayStack.h\"
#include <stdlib.h>
#include \"StackException.h\"
using namespace std;
int main()
{
ArrayStack<int> arrayStack;
arrayStack.push(7);
arrayStack.push(13);
cout << \"Top element is : \" << arrayStack.top() << endl;
arrayStack.pop();
arrayStack.push(9);
cout << \"Top element is : \" << arrayStack.top() << endl;
cout << \"Top element is : \" << arrayStack.top() << endl;
arrayStack.pop();
return EXIT_SUCCESS;
}

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 <iostream>
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 <iostream>
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 <iostream>
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 <iostream>
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 <iostream>

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site