diff options
Diffstat (limited to 'linkedlist/stack.c')
-rw-r--r-- | linkedlist/stack.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/linkedlist/stack.c b/linkedlist/stack.c new file mode 100644 index 0000000..de77b72 --- /dev/null +++ b/linkedlist/stack.c @@ -0,0 +1,38 @@ +#include "stack.h" + +void stack_add(void* _data, struct stack** _stackpointer) +{ + struct stack* newsp = malloc(sizeof(*newsp)); + + newsp->data = _data; + newsp->next = *_stackpointer; + *_stackpointer = newsp; +} + +void* stack_get(struct stack** _stackpointer) +{ + void* return_data; + struct stack* oldsp; //Old stackpointer + + if(!*_stackpointer) + return NULL; + + return_data = (*_stackpointer)->data; + oldsp = *_stackpointer; + *_stackpointer = (*_stackpointer)->next; + free(oldsp); + + return return_data; +} + +void stack_init(struct stack** _stackpointer) +{ + stack_flush(_stackpointer); + *_stackpointer = NULL; +} + +void stack_flush(struct stack** _stackpointer) +{ + while(*_stackpointer) + stack_get(_stackpointer); +} |