#ifndef CPP_STACK_HPP
#define CPP_STACK_HPP 1

#include <cassert>
#include <iostream>

class Stack
{
    public:
        Stack()
            : top(nullptr)
        {
        }

        ~Stack()
        {
            while(!empty()) {
                pop();
            }
        }

        bool empty() const
        {
            return !top;
        }

        void push(double value)
        {
            Element *add = new Element;
            add->last    = top;
            add->value   = value;
            top          = add;
        }

        double pop()
        {
            assert(!empty());

            Element *remove = top;
            top             = remove->last;
            double  value   = remove->value;
            delete remove;
            return value;
        }

        void print(const char *txt) const
        {
            std::cout << txt << ": ";
            print_elements(top);
            std::cout << ";" << std::endl;
        }

    private:
        struct Element
        {
            double      value;
            Element     *last;
        };

        void print_elements(const Element *top) const
        {
            if (top) {
                print_elements(top->last);
                std::cout << "   " << top->value;
            }
        }

        Element     *top;
};

#endif // CPP_STACK_HPP
