Archeus 0.0.0
A C library and game engine that focuses on documentation
Loading...
Searching...
No Matches
stack.c
Go to the documentation of this file.
1#include "arc/std/stack.h"
2
3#include "arc/std/errno.h"
4#include <stdlib.h>
5
7
12
17
19 *stack = (ARC_Stack *)malloc(sizeof(ARC_Stack));
20 (*stack)->currentSize = 0;
21 (*stack)->node = NULL;
22}
23
25 if(stack->currentSize != 0 || stack->node != NULL){
27 ARC_DEBUG_LOG_ERROR("ARC_Stack_Destroy(stack) called, but stack was not empty");
28 return;
29 }
30
31 free(stack);
32}
33
34void ARC_Stack_Push(ARC_Stack *stack, void *data){
35 if(stack->currentSize == ~(uint32_t)0){
37 ARC_DEBUG_LOG_ERROR("ARC_Stack_Push(stack) called, size of stack is maxed, cannot add another node");
38 return;
39 }
40
41 ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode));
42 node->data = data;
43 node->next = NULL;
44
45 node->next = stack->node;
46 stack->node = node;
47
48 stack->currentSize++;
49}
50
52 if(stack->currentSize == 0){
54 ARC_DEBUG_LOG_ERROR("ARC_Stack_Pop(stack) called, but stack was not empty");
55 return NULL;
56 }
57
58 ARC_StackNode *node = stack->node;
59 if(node == NULL){
61 ARC_DEBUG_LOG_ERROR("ARC_Stack_Pop(stack) called, but node was NULL");
62 return NULL;
63 }
64
65 stack->node = node->next;
66 void *nodeData = node->data;
67 free(node);
68
69 stack->currentSize--;
70
71 return nodeData;
72}
73
74uint32_t ARC_Stack_Size(ARC_Stack *stack){
75 return stack->currentSize;
76}
int32_t arc_errno
Definition errno.c:5
#define ARC_ERRNO_OVERFLOW
Definition errno.h:10
#define ARC_ERRNO_NULL
Definition errno.h:6
#define ARC_DEBUG_LOG_ERROR(STR)
Definition errno.h:39
#define ARC_ERRNO_DATA
Definition errno.h:7
uint32_t ARC_Stack_Size(ARC_Stack *stack)
gets size of stack
Definition stack.c:74
void ARC_Stack_Create(ARC_Stack **stack)
creates ARC_Stack type
Definition stack.c:18
void ARC_Stack_Push(ARC_Stack *stack, void *data)
pushes value on stack
Definition stack.c:34
void * ARC_Stack_Pop(ARC_Stack *stack)
pops top off of ARC_Stack
Definition stack.c:51
void ARC_Stack_Destroy(ARC_Stack *stack)
destroyes ARC_Stack type
Definition stack.c:24
ARC_StackNode * next
Definition stack.c:9
void * data
Definition stack.c:10
uint32_t currentSize
Definition stack.c:14
ARC_StackNode * node
Definition stack.c:15