added stack datatype to std
This commit is contained in:
parent
875b69c407
commit
c3b4a4e209
5 changed files with 145 additions and 0 deletions
70
src/std/stack.c
Normal file
70
src/std/stack.c
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include "arc/std/stack.h"
|
||||
|
||||
#include "arc/std/errno.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct ARC_StackNode ARC_StackNode;
|
||||
|
||||
struct ARC_StackNode {
|
||||
ARC_StackNode *next;
|
||||
void *data;
|
||||
};
|
||||
|
||||
struct ARC_Stack {
|
||||
uint32_t currentSize;
|
||||
ARC_StackNode *node;
|
||||
};
|
||||
|
||||
void ARC_Stack_Create(ARC_Stack **stack){
|
||||
*stack = (ARC_Stack *)malloc(sizeof(ARC_Stack));
|
||||
(*stack)->currentSize = 0;
|
||||
(*stack)->node = NULL;
|
||||
}
|
||||
|
||||
void ARC_Stack_Destroy(ARC_Stack *stack){
|
||||
if(stack->currentSize != 0 || stack->node != NULL){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
ARC_DEBUG_ERR("ARC_Stack_Destroy called, but stack was not empty");
|
||||
return;
|
||||
}
|
||||
|
||||
free(stack);
|
||||
}
|
||||
|
||||
void ARC_Stack_Push(ARC_Stack *stack, void *data){
|
||||
ARC_StackNode *node = (ARC_StackNode *)malloc(sizeof(ARC_StackNode));
|
||||
node->data = data;
|
||||
node->next = NULL;
|
||||
|
||||
node->next = stack->node;
|
||||
stack->node = node;
|
||||
|
||||
stack->currentSize++;
|
||||
}
|
||||
|
||||
void *ARC_Stack_Pop(ARC_Stack *stack){
|
||||
if(stack->currentSize == 0){
|
||||
arc_errno = ARC_ERRNO_DATA;
|
||||
ARC_DEBUG_ERR("ARC_Stack_Pop called, but stack was not empty");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ARC_StackNode *node = stack->node;
|
||||
if(node == NULL){
|
||||
arc_errno = ARC_ERRNO_NULL;
|
||||
ARC_DEBUG_ERR("ARC_Stack_Pop called, but node was NULL");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
stack->node = node->next;
|
||||
void *nodeData = node->data;
|
||||
free(node);
|
||||
|
||||
stack->currentSize--;
|
||||
|
||||
return nodeData;
|
||||
}
|
||||
|
||||
uint32_t ARC_Stack_Size(ARC_Stack *stack){
|
||||
return stack->currentSize;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue