Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.75 KB | None | 0 0
  1. #define RESZ_AMT 0x100
  2.  
  3. typedef struct {
  4.     void **item;
  5.     size_t sz, rsz;
  6. } stack_t;
  7.  
  8. stack_t stack_init()
  9. {
  10.     stack_t this;
  11.     this.item = malloc(this.rsz = RESZ_AMT);
  12.     this.sz = 0;
  13. }
  14.  
  15. void stack_push(stack_t *this, void *item)
  16. {
  17.     if (this->sz == this->rsz) {
  18.         this->item = realloc(this->item, (this->rsz += RESZ_AMT)
  19.                                          * sizeof(void *));
  20.     }
  21.     this->item[this->sz++] = item;
  22. }
  23.  
  24. void *stack_pop(stack_t *this)
  25. {
  26.     if (this->sz == 0) return NULL;
  27.  
  28.     if (--this->sz && this->sz = this->rsz - RESZ_AMT) {
  29.         this->item = realloc(this->item, (this->rsz -= RESZ_AMT)
  30.                                          * sizeof(void *));
  31.     }
  32.     return this->item[this->sz];
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement