Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define RESZ_AMT 0x100
- typedef struct {
- void **item;
- size_t sz, rsz;
- } stack_t;
- stack_t *stack_init()
- {
- stack_t this = malloc(sizeof(stack_t));
- this->item = malloc(this->rsz = RESZ_AMT);
- this->sz = 0;
- return this;
- }
- void stack_push(stack_t *this, void *item)
- {
- if (this->sz == this->rsz) {
- this->item = realloc(this->item, (this->rsz += RESZ_AMT)
- * sizeof(void *));
- }
- this->item[this->sz++] = item;
- }
- void *stack_pop(stack_t *this)
- {
- if (this->sz == 0) return NULL;
- if (--this->sz && this->sz = this->rsz - RESZ_AMT) {
- this->item = realloc(this->item, (this->rsz -= RESZ_AMT)
- * sizeof(void *));
- }
- return this->item[this->sz];
- }
- void stack_free(stack_t *this)
- {
- free(this->item);
- free(this);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement