Write a wrapper class implementation of a Stack that uses the ExtendableVector c
ID: 3929743 • Letter: W
Question
Write a wrapper class implementation of a Stack that uses the ExtendableVector class in the class GitHub page (https://github.com/apanangadan/CSUF-CPSC_131/blob/master/ExtendableVector.h). You only need to implement these public methods:
#include “ExtendableVector.h”
template <typename E>
class ExtendableVectorStack {
public:
ExtendableVectorStack (); // constructor: no need to specify capacity!
int size(); // number of items in the stack
bool empty(); // is the stack empty?
E& top(); // get the top element
void push(E& e); // push element onto stack
void pop(); // pop the stack
private:
// “wrap” the ExtendableVector here along with other variables necessary to implement the public methods
};
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
#include “ExtendableVector.h”
template
class ExtendableVectorStack {
public:
ExtendableVectorStack (); // constructor: no need to specify capacity!
int size(); // number of items in the stack
bool empty(); // is the stack empty?
E& top(); // get the top element
void push(E& e); // push element onto stack
void pop(); // pop the stack
private:
ExtendableVector *stack;
};
// Implementation
ExtendableVectorStack::ExtendableVectorStack(){
// creating object of ExtendableVector
stack = new ExtendableVector();
}
// number of items in the stack
int ExtendableVectorStack::size(){
return stack->size(); // calling ExtendableVector's size() method
}
// is the stack empty?
bool ExtendableVectorStack::empty(){
// calling ExtendableVector's empty() method
return stack->empty();
}
// get the top element
E& ExtendableVectorStack::top(){
if(empty()){
throw range_error("index out of bounds");
}
return stack->at(size()-1); // getting top element
}
// push element onto stack
void ExtendableVectorStack::push(E& e){
stack->insert(size(), e); // calling insert method of ExtendableVector
}
// pop the stack
void ExtendableVectorStack::pop(){
stack->erase(size()-1); // erasing top element
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.