stack.c (602B)
1 #include "stack.h" 2 3 struct stack *stack_new() 4 { 5 const size_t siz = 1024; 6 7 struct stack *s = malloc(sizeof(struct stack *)); 8 s->s = malloc(siz); 9 s->sp = -1; 10 s->siz = siz; 11 12 return s; 13 } 14 15 int stack_isempty(struct stack *s) 16 { 17 return (s->sp == -1); 18 } 19 20 int stack_isfull(struct stack *s) 21 { 22 return (s->sp == s->siz); 23 } 24 25 int stack_peek(struct stack *s) 26 { 27 if (!stack_isempty(s)) 28 return s->s[s->sp]; 29 return 0; 30 } 31 32 void stack_push(struct stack *s, int item) 33 { 34 if (!stack_isfull(s)) 35 s->s[++s->sp] = item; 36 } 37 38 int stack_pop(struct stack *s) 39 { 40 if (!stack_isempty(s)) 41 return s->s[s->sp--]; 42 return 0; 43 }