In: Computer Science
Implement a generic MyStack class using templates. A stack allows elements to be added and removed in a last-in, first-out (LIFO) manner. Stacks have an operation called push to place elements at the top of the stack, and another operation called pop to remove and return the element at the top of the stack. The only element on the stack that may be referenced is the one on the top. This means that if two elements are pushed onto the stack, the top most element must be removed from the stack before the first pushed element can be referenced. Your MyStack class must implement the following member functions (see below) in a file named MyStack.hpp.
(a) A member function push that takes the element to be pushed onto the stack and returns nothing.
(b) A member function pop that takes no arguments and returns the top element.
(c) A member function top that takes no arguments and returns the top element without removing it from the stack.
(d) A member function empty that takes no arguments and returns true if the stack is empty and false otherwise.
(e) A member function size that takes no arguments and returns the number of elements on the stack.
You are free to choose between a singly linked list (SLinkedList) and a vector as the internal data structure of your MyStack class. You can use the following code as a test driver for your implementation.
#include
#include
#include"MyStack.hpp"
using namespace s t d ;
int main ( ) { MyStack i n t s t a c k ;
MyStack s t r s t a c k ;
i n t s t a c k . push ( 2 ) ;
i n t s t a c k . push ( 7 ) ;
i n t s t a c k . push ( 1 ) ;
i n t s t a c k . push ( 8 ) ;
i n t s t a c k . push ( 2 ) ;
s t r s t a c k . push ( " Hermione_Granger " ) ;
s t r s t a c k . push ( " Ron_Weasley " ) ;
s t r s t a c k . push ( " Fleur_Delacour " ) ;
s t r s t a c k . push ( " Draco_Malfoy " ) ;
s t r s t a c k . push ( " Cedric_Diggory " ) ;
c ou t << "stack size: " << s t r s t a c k . s i z e ( ) << e n dl ;
c ou t << "popped: " << s t r s t a c k . pop ( ) << e n dl ;
c ou t << "peek: " << s t r s t a c k . top ( ) << e n dl ;
c ou t << "popped: " << s t r s t a c k . pop ( ) << e n dl ;
c ou t << "stack size: " << s t r s t a c k . s i z e ( ) << e n dl ;
c ou t << "stack empty: " << ( i n t s t a c k . empty ( ) ? "Y" : "N" ) << e n dl ;
while ( ! i n t s t a c k . empty ( ) ) {
c ou t << i n t s t a c k . pop ( ) << " " ;
} c ou t << e n dl ;
c ou t << "stack empty: " << ( i n t s t a c k . empty ( ) ? "Y" : "N" ) << e n dl ;
return 0 ;
}
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
//MyStack.hpp file
#ifndef MyStack_H
#define MyStack_H
#include<vector>
template <typename T>
class MyStack{
private:
//using a vector as underlying data structure
std::vector<T> stk;
public:
//adds an element to the top (back)
void push(T element);
//removes and returns the top element
T pop();
//returns the top element, without removing
T top() const;
//returns true if stack is empty
bool empty() const;
//returns the current number of elements
int size() const;
};
//implementation of all methods
template <typename T>
void MyStack<T>::push(T element){
//simply adding to the back
stk.push_back(element);
}
template <typename T>
T MyStack<T>::pop(){
//getting last element
T element=stk[stk.size()-1];
//removing it
stk.pop_back();
//returning it
return element;
}
template <typename T>
T MyStack<T>::top() const{
//getting and returning last element
T element=stk[stk.size()-1];
return element;
}
template <typename T>
bool MyStack<T>::empty() const{
return stk.empty();
}
template <typename T>
int MyStack<T>::size() const{
return stk.size();
}
#endif
/*OUTPUT using your test program*/
stack size: 5
popped: Cedric_Diggory
peek: Draco_Malfoy
popped: Draco_Malfoy
stack size: 3
stack empty: N
2 8 1 7 2
stack empty: Y