A sample implementation: istack.c
# include <stdlib.h>
# include <stdio.h>
# include "istack.h"
# define INCREMENT 64
struct istack {
size_t total;
size_t height;
int *array;
};
istack istack_new (void)
{
istack s = malloc (sizeof (struct istack));
if (s == NULL)
return NULL;
s->total = 0;
s->height = 0;
s->array = NULL;
return s;
}
int istack_push (istack s, int i)
{
if (s->height >= s->total) {
int *a = realloc (s->array, (s->total + INCREMENT) * sizeof (int));
if (a == NULL)
return -1; /* couldn't allocate more space */
s->array = a;
s->total += INCREMENT;
}
s->array [s->height++] = i;
return 0; /* worked out ok */
}
int istack_height (istack s)
{
return s->height;
}
int istack_pop (istack s)
{
if (s->height > 0)
return s->array [--s->height];
else {
fputs ("Fell off the end of the stack.\n", stderr);
exit (2);
}
}
void istack_delete (istack s)
{
if (s->array != NULL)
free (s->array);
free (s);
}