/* Stack implementation file: stack.c++ author: Jakob Klepp date: 2017-03-09 */ #include #include "stack.h++" struct t_layer { int value; struct t_layer* below; }; Stack Stack_new() { Stack stack = 0; return stack; } void Stack_destroy(Stack* stack) { while (!Stack_isEmpty(stack)) { Stack_pop(stack); } } int Stack_peek(Stack* stack) { return (*stack)->value; } bool Stack_isEmpty(Stack* stack) { return 0 == (*stack); } int Stack_pop(Stack* stack) { int value = (*stack)->value; struct t_layer* below = (*stack)->below; delete *stack; (*stack) = below; return value; } void Stack_push(Stack* stack, int value) { struct t_layer* newTop = new struct t_layer; newTop->below = *stack; newTop->value = value; *stack = newTop; }