#include "stack.h" #include #include stack_t init_stack(void) { stack_t s; s.p = malloc((s.cap = 128) * sizeof(int)); assert(s.p); s.top = -1; return s; } void push(stack_t *stack, int v) { if (stack->top >= stack->cap) { stack->p = realloc(stack->p, (stack->cap += 128) * sizeof(int)); assert(stack->p); } stack->top++; stack->p[stack->top] = v; } int pop(stack_t *stack) { return stack->p[stack->top--]; } int empty(stack_t stack) { return stack.top < 0; } int top(stack_t stack) { return stack.p[stack.top]; } void free_stack(stack_t *s) { if (s->p) free(s->p); s->p = NULL; }