Make the library when building dist and normal targets
[omfsprogs.git] / stack.c
blob41d3e4c3e38a97ddf58bc116ecec42618b85874a
1 /* linked list stack */
3 #include "stack.h"
4 #include <stdlib.h>
6 stack_t *stack_init()
8 stack_t *stack = malloc(sizeof(stack));
9 stack->next = NULL;
10 return stack;
13 int stack_empty(stack_t *stack)
15 return (stack->next == NULL);
18 int stack_push(stack_t *stack, void *user)
20 stack_t *new = malloc(sizeof(stack));
21 if (!new)
22 return 0;
24 stack_t *tmp = stack->next;
25 stack->next = new;
26 new->next = tmp;
27 new->data = user;
28 return 1;
31 void *stack_pop(stack_t *stack)
33 void *data;
34 if (!stack->next) return NULL;
36 stack_t *tmp = stack->next;
37 stack->next = tmp->next;
39 data = tmp->data;
40 free(tmp);
41 return data;
44 void stack_destroy(stack_t *stack)
46 while (!stack_empty(stack))
47 stack_pop(stack);
49 free(stack);