aboutsummaryrefslogtreecommitdiff
path: root/stack.c
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-03-29 14:52:47 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-03-29 14:52:47 +0300
commit5e7591a2f684bba637e120497a2ba0fdd3db8dda (patch)
tree344e55bf37a8bae5a31f231108a20bd57a3f4cb8 /stack.c
parent7a69d0103dadc08f228d14b27d38df193044bb29 (diff)
downloadastar-5e7591a2f684bba637e120497a2ba0fdd3db8dda.tar.xz
Make the stack dynamic
Diffstat (limited to 'stack.c')
-rw-r--r--stack.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/stack.c b/stack.c
index d719cb3..8b4c478 100644
--- a/stack.c
+++ b/stack.c
@@ -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);
+}