Skip to content

Instantly share code, notes, and snippets.

@gyakoo
Created August 21, 2015 17:53
Show Gist options
  • Save gyakoo/89c6d08b765d1d37556c to your computer and use it in GitHub Desktop.
Save gyakoo/89c6d08b765d1d37556c to your computer and use it in GitHub Desktop.
Fixed heap array C for Stack ADT
typedef struct flt_stack
{
void** entries;
int count;
int capacity;
}flt_stack;
int flt_stack_create(flt_stack** s, int capacity)
{
flt_stack* _s = (flt_stack*)flt_calloc(1,sizeof(flt_stack));
if ( !_s ) return 0;
if (!capacity)
capacity = FLT_STACKARRAY_SIZE;
_s->entries = (void**)flt_malloc(sizeof(void*)*capacity);
if (!_s->entries) return 0;
_s->capacity = capacity;
*s = _s;
return 1;
}
void flt_stack_destroy(flt_stack** s)
{
if ( !s || !*s ) return;
flt_safefree((*s)->entries);
flt_safefree((*s));
}
void flt_stack_clear(flt_stack* s)
{
s->count=0;
}
void flt_stack_push(flt_stack* s, void* value)
{
if (s->count >= s->capacity )
{
FLT_BREAK;
return;
}
s->entries[s->count++]=value;
}
void* flt_stack_top(flt_stack* s)
{
return s->count ? s->entries[s->count-1] : fltnull;
}
void* flt_stack_pop(flt_stack* s)
{
void* v=fltnull;
if (s->count)
{
v=s->entries[--s->count];
}
return v;
}
int flt_stack_set_top_if_null(flt_stack* s, void* value)
{
int r=0;
if (s->count && !s->entries[s->count-1] )
{
s->entries[s->count-1]=value;
r=1;
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment