#ifndef _STACK_H_ #define _STACK_H_ #include using data_t = std::string; // you must correct the stack to store integers class Stack { public: Stack(); ~Stack(); Stack(Stack const&); Stack& operator=(Stack const&); bool isEmpty() const; void push(std::string const&); std::string pop(); std::string& top(); std::string const& top() const; private: class Element { public: Element(std::string const&, Element* n = nullptr); Element(Element const&) = delete; ~Element(); Element& operator=(Element const&) = delete; static Element* clone(Element const*); Element* next; std::string data; }; Element* mtop; }; #endif #include #include #include "stack.h" Stack::Stack() : mtop(nullptr) {} // intentionally empty Stack::Stack(Stack const& s) : mtop(Stack::Element::clone(s.mtop)) {} // intentionally empty Stack::~Stack() { delete mtop; } Stack& Stack::operator=(Stack const& rhs) { if ( this != &rhs ) { Stack copy(rhs); std::swap(copy.mtop, mtop); } return *this; } bool Stack::isEmpty() const { return (mtop == nullptr); } void Stack::push(std::string const& d) { mtop = new Element(d, mtop); } std::string Stack::pop() { std::string retval = top(); Element* victim = mtop; mtop = mtop->next; victim->next = nullptr; delete victim; return retval; } std::string& Stack::top() { if (mtop != nullptr) return mtop->data; else throw std::exception(); } std::string const& Stack::top() const { if (mtop != nullptr) return mtop->data; else throw std::exception(); } Stack::Element::Element(std::string const& d, Stack::Element* n) : next(n), data(d) { // intentionally empty } Stack::Element::~Element() { delete next; } Stack::Element* Stack::Element::clone(Element const* s) { // you will have to implement this! // three lines (or more) missing! return nullptr; } #include #include "stack.h" int main() { Stack reverse; std::string n; std::cout << "Mata in heltal: "; while ( std::cin >> n ) reverse.push(n); Stack inorder; // you must fill the inorder stack to get expected output below! std::cout << "Din inmatning var (i rätt ordning): " << std::endl; while ( ! inorder.isEmpty() ) std::cout << inorder.pop() << std::endl; std::cout << "Din inmatning var (i omvänd ordning): " << std::endl; while ( ! reverse.isEmpty() ) std::cout << reverse.pop() << std::endl; return 0; }