/* main.c, test my stack of strings */ #include #include unsigned int times( unsigned int ier, unsigned int cand ); /* interface specification for a stack of strings */ void push( const char * s ); /* push s on the stack */ const char * pop(); /* returns the top of stack and pops */ int empty(); /* reports 1 if stack empty, else 0 */ unsigned int main() { /* run the test */ push( " zebra.\n" ); push( " pet" ); push( " my" ); push( " like" ); push( "I" ); while( !empty() ) { printf( "%s", pop() ); } return 0; } /* implement a stack of strings */ struct stack { const char * str; /* the string in this stack element */ struct stack * next; /* link to rest of stack */ }; struct stack * top = NULL; void push( const char * s ) { /* push s on the stack */ struct stack * new = malloc( sizeof( struct stack ) ); new->str = s; new->next = top; top = new; } const char * pop() { /* returns the top of stack and pops */ const char * ret = top->str; struct stack * old = top; top = top->next; free( old ); return ret; } int empty() { /* reports 1 if stack empty, else 0 */ return top == NULL; }