stack.c (433B)
1 #include <u.h> 2 #include <libc.h> 3 #include "stack.h" 4 5 int 6 push(Stack *S, void *v) 7 { 8 S->data = realloc(S->data, (S->size + 1) * sizeof(void*)); 9 S->data[S->size] = v; 10 S->size++; 11 return 1; 12 } 13 14 void* 15 pop(Stack *S) 16 { 17 void *v; 18 S->size--; 19 v = S->data[S->size]; 20 S->data = realloc(S->data, S->size * sizeof(void*)); 21 return v; 22 } 23 24 Stack* 25 newstack(void) 26 { 27 Stack *R; 28 R = malloc(sizeof(Stack)); 29 R->size = 0; 30 R->data = nil; 31 return R; 32 }