aboutsummaryrefslogtreecommitdiff
path: root/src/position_stack.c
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-01-01 13:51:51 +0100
committerHampusM <hampus@hampusmat.com>2022-01-01 13:51:51 +0100
commit1bed3ac57906b26ef05b25c2bc5c1dca424dba4a (patch)
treebd445f7800d27112b3c45199c797e8a048b0306b /src/position_stack.c
parent31c6239cb2fcb75aa2ec846ce88bd57a631bbd32 (diff)
refactor: fix memory leaks & general improvements
Diffstat (limited to 'src/position_stack.c')
-rw-r--r--src/position_stack.c36
1 files changed, 21 insertions, 15 deletions
diff --git a/src/position_stack.c b/src/position_stack.c
index 90ace87..846d933 100644
--- a/src/position_stack.c
+++ b/src/position_stack.c
@@ -19,51 +19,57 @@ void stack_error(int err)
}
// Creates a new stack
-struct PositionStack *create_pos_stack(int capacity)
+PositionStack *pos_stack_create(int capacity)
{
- struct PositionStack *stack_pt = malloc(sizeof(struct PositionStack));
+ PositionStack *pos_stack = malloc(sizeof(PositionStack));
- stack_pt->capacity = capacity;
- stack_pt->top = -1;
- stack_pt->items = malloc(sizeof(struct Position) * capacity);
+ pos_stack->capacity = capacity;
+ pos_stack->top = -1;
+ pos_stack->items = malloc(sizeof(Position) * capacity);
- return stack_pt;
+ return pos_stack;
+}
+
+void pos_stack_destroy(PositionStack *pos_stack)
+{
+ free(pos_stack->items);
+ free(pos_stack);
}
// Adds a new item to a stack
-void pos_stack_push(struct PositionStack *stack_pt, struct Position pos)
+void pos_stack_push(PositionStack *pos_stack, Position pos)
{
// Avoid a overflow by checking if the stack is full
- if (stack_pt->top == stack_pt->capacity - 1)
+ if (pos_stack->top == pos_stack->capacity - 1)
{
stack_error(STACK_ERR_OVERFLOW);
}
// Add an element and increase the top index
- stack_pt->items[++stack_pt->top] = pos;
+ pos_stack->items[++pos_stack->top] = pos;
}
// Returns the topmost item of a stack
-struct Position pos_stack_peek(struct PositionStack *stack_pt)
+Position pos_stack_peek(PositionStack *pos_stack)
{
// Avoid a underflow by checking if the stack is empty
- if (stack_pt->top == -1)
+ if (pos_stack->top == -1)
{
stack_error(STACK_ERR_UNDERFLOW);
}
- return stack_pt->items[stack_pt->top];
+ return pos_stack->items[pos_stack->top];
}
// Deletes the topmost item of a stack
-struct Position pos_stack_pop(struct PositionStack *stack_pt)
+Position pos_stack_pop(PositionStack *pos_stack)
{
// Avoid a underflow by checking if the stack is empty
- if (stack_pt->top == -1)
+ if (pos_stack->top == -1)
{
stack_error(STACK_ERR_UNDERFLOW);
}
// Decrease the stack size by 1 and return the popped element
- return stack_pt->items[stack_pt->top--];
+ return pos_stack->items[pos_stack->top--];
}