#include #include #include "stack.h" struct stack_element { int value; struct stack_element * next; }; struct stack { struct stack_element * top; }; struct stack * stack_new() { struct stack * this = malloc(sizeof(struct stack)); if (this) this->top = 0; return this; } void stack_destroy(struct stack * this) { struct stack_element * e = this->top; while (e) { struct stack_element * tmp = e; e = e->next; free(tmp); } free(this); } int stack_push(struct stack * this, int v) { struct stack_element * e = malloc(sizeof(struct stack_element)); if (e) { e->value = v; e->next = this->top; this->top = e; return 1; } else return 0; } int stack_pop(struct stack * this) { struct stack_element * tmp = this->top; if (!tmp) { return STACK_UNDERFLOW; } else { int result = tmp->value; this->top = tmp->next; free(tmp); return result; } } void stack_print(const struct stack * this) { for(struct stack_element * e = this->top; e; e = e->next) printf("%d\n", e->value); }