/* main.c -- program to demonstrate use of a stack */ #include #include "stack.h" struct stackElement { int item; struct stackElement * next; }; /* methods the constructor needs to know about */ static void stackPush( Stack s, int i ); static int stackPop( Stack s ); /* constructor */ Stack newStack() { Stack new = malloc( sizeof( Stack* ) ); new->pushMethod = &stackPush; new->popMethod = &stackPop; new->sp = NULL; return new; } /* methods */ static void stackPush( Stack s, int i ) { /* push i on stack s */ struct stackElement * new = malloc( sizeof( struct stackElement ) ); new->item = i; new->next = s->sp; s->sp = new; } static int stackPop( Stack s ) { /* pop an element from stack s and return it */ struct stackElement * old = s->sp; int i = old->item; s->sp = old->next; free( old ); return i; }