Question
I need help with finishing the code for 'stack by means of an array' I need to include the following steps into the main function
I need help with finishing the code for 'stack by means of an array'
I need to include the following steps into the main function of 'stack.cpp' :
2) Add to the main() in stack.cpp to declare an ArrayStack of string type and only 10 elements. Execute the interface commands below in order:
a. push(Bob)
b. push(Alice);
c. print the top elements
d. pop()
e. push(Eve)
f. print the top element
g. print the top element
h. pop()
// 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
// stack.cpp
#include #include #include "ArrayStack.h" #include #include "StackException.h" using namespace std; int main() { ArrayStack 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();
(It needs to include after this point)
return EXIT_SUCCESS; }
Anyone like to help out to finish? Thank you
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