1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
#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(Stackconst this_)
{
    this_->top_nullptr;
}

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

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

double stack_pop(Stackconst 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(Stackconst this_)
{
    while (!stack_empty(this_)) {
        stack_pop(this_);
    }
}

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

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

#endif // C_STYLE_STACK_HPP