diff options
author | HampusM <hampus@hampusmat.com> | 2022-01-09 21:47:23 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-01-09 21:47:23 +0100 |
commit | 8ceb79db1d0687bba005cef4a77bb889bf7ec3c3 (patch) | |
tree | b7c13359f652506d60c8556ea386ae8d50bfc5bc /src/position_stack.c | |
parent | 097aa95c1f0cb159e7d9d0a3edf9284c421ee298 (diff) |
refactor: rewrite to c++
Diffstat (limited to 'src/position_stack.c')
-rw-r--r-- | src/position_stack.c | 76 |
1 files changed, 0 insertions, 76 deletions
diff --git a/src/position_stack.c b/src/position_stack.c deleted file mode 100644 index 0b4895a..0000000 --- a/src/position_stack.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "position_stack.h" -#include "utils.h" -#include <stdio.h> -#include <stdlib.h> - -// Error handler for stack errors -void stack_error(int err) -{ - switch (err) - { - case STACK_ERR_OVERFLOW: - printf("Error: Stack overflow\nBe kind and report this problem."); - break; - case STACK_ERR_UNDERFLOW: - printf("Error: Stack underflow\nBe kind and report this problem."); - break; - } - - exit(1); -} - -// Creates a new stack -PositionStack *pos_stack_create(unsigned int capacity) -{ - PositionStack *pos_stack = malloc_s(sizeof(PositionStack)); - - pos_stack->capacity = capacity; - pos_stack->top = -1; - pos_stack->items = malloc_s(sizeof(Position) * capacity); - - 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(PositionStack *pos_stack, Position pos) -{ - // Avoid a overflow by checking if the stack is full - if (pos_stack->top == pos_stack->capacity - 1U) - { - stack_error(STACK_ERR_OVERFLOW); - } - - // Add an element and increase the top index - pos_stack->items[++pos_stack->top] = pos; -} - -// Returns the topmost item of a stack -Position pos_stack_peek(PositionStack *pos_stack) -{ - // Avoid a underflow by checking if the stack is empty - if (pos_stack->top == -1) - { - stack_error(STACK_ERR_UNDERFLOW); - } - - return pos_stack->items[pos_stack->top]; -} - -// Deletes the topmost item of a stack -Position pos_stack_pop(PositionStack *pos_stack) -{ - // Avoid a underflow by checking if the stack is empty - if (pos_stack->top == -1) - { - stack_error(STACK_ERR_UNDERFLOW); - } - - // Decrease the stack size by 1 and return the popped element - return pos_stack->items[pos_stack->top--]; -} |