#ifndef C_STYLE_STACK_HPP
#define C_STYLE_STACK_HPP 1

#include <cassert>
#include <iostream>

struct Stack_Element
{
    double          value;
    Stack_Element   *last;
};

struct Stack
{
    Stack_Element   *top_;
};

void stack_construct_default(Stack * const this_)
{
    this_->top_ = nullptr;
}

bool stack_empty(const Stack * const this_)
{
    return !(this_->top_);
}

void stack_push(Stack * const this_, double value)
{
    Stack_Element *add = new Stack_Element;
    add->last          = this_->top_;
    add->value         = value;
    this_->top_        = add;
}

double stack_pop(Stack * const this_)
{
    assert(!stack_empty(this_));

    Stack_Element *remove = this_->top_;
    this_->top_           = remove->last;
    double  value         = remove->value;
    delete remove;
    return value;
}

void stack_destruct(Stack * const this_)
{
    while (!stack_empty(this_)) {
        stack_pop(this_);
    }
}

void stack_print_elements_(const Stack * const this_, const Stack_Element *top)
{
    if (top) {
        stack_print_elements_(this_, top->last);
        std::cout << "   " << top->value;
    }
}

void stack_print(const Stack * const this_, const char *txt)
{
    std::cout << txt << ": ";
    stack_print_elements_(this_, this_->top_);
    std::cout << ";" << std::endl;
}

#endif // C_STYLE_STACK_HPP
