#include "stack.hpp"
#include <cassert> #include <iostream> #include <utility> void Stack_construct_default(Stack * const this_) { this_->top = nullptr; } void Stack_construct_copy(Stack * const this_, const Stack * const stack) { // start with default constructed stack Stack_construct_default(this_); // then fill it with a copy Stack_copy_elements_(this_, stack->top); } void Stack_construct_swap(Stack * const this_, Stack * const stack) { std::swap(this_->top, stack->top); } void Stack_destruct(Stack * const this_) { while (!Stack_isEmpty(this_)) { Stack_pop(this_); } } bool Stack_isEmpty(const Stack * const this_) { return !this_->top; } Stack * const Stack_assign(Stack * const this_, Stack * const stack) { std::swap(this_->top, stack->top); return this_; } void Stack_push(Stack * const this_, double value) { Stack_Element *add = new Stack_Element; add->value = value; add->last = this_->top; this_->top = add; } double Stack_pop(Stack * const this_) { assert(!Stack_isEmpty(this_)); Stack_Element *remove = this_->top; this_->top = remove->last; double value = remove->value; delete remove; return value; } void Stack_print(Stack * const this_, const char *txt) { if (txt) { std::cout << txt << std::endl; } else { std::cout << "Stack contains:" << std::endl; } Stack_print_elements_(this_, this_->top); std::cout << std::endl; } void Stack_copy_elements_(Stack * const this_, const Stack_Element *element) { if (element) { Stack_copy_elements_(this_, element->last); Stack_push(this_, element->value); } } void Stack_print_elements_(const Stack * const this_, const Stack_Element *element) { if (element) { Stack_print_elements_(this_, element->last); std::cout << element->value << " "; } } |