diff options
Diffstat (limited to 'stack.c')
| -rw-r--r-- | stack.c | 10 |
1 files changed, 9 insertions, 1 deletions
@@ -6,12 +6,17 @@ PositionStack ps_new(void) { PositionStack ps; + ps.capacity = STACK_INITIAL_CAPACITY; + ps.arr = malloc(sizeof(Position) * ps.capacity); ps.top = 0; return ps; } int ps_push(PositionStack *ps, Position pos) { - if (ps->top >= STACK_SIZE) return -1; /* FIXME: do a dynamic stack */ + if (ps->top >= ps->capacity) { + ps->capacity *= STACK_SIZE_COEFFICIENT; + if ((ps->arr = realloc(ps->arr, sizeof(Position) * ps->capacity)) == NULL) return -1; + } ps->arr[ps->top] = pos; ps->top += 1; return 0; @@ -28,3 +33,6 @@ Position ps_peek(PositionStack ps) { return ps.arr[ps.top - 1]; } +void ps_free(PositionStack ps) { + free(ps.arr); +} |
