# Shell archive made by dwjones on Mon Oct 23 10:00:28 AM CDT 2023 # To install this software on a UNIX system: # 0) save this text in a file named, for example, shar, on that system # 1) create a directory (e.g. with the shell command mkdir stack) # 2) change to that directory (e.g. with the command cd stack), # 3) direct the remainder of this text to sh (e.g. sh < ../shar). # This will make sh create files in the new directory; it will do # nothing else (if you're paranoid, you should scan the following text # to verify this before you follow these directions). cat > main.c <<\xxxxxxxxxx /* main.c -- test my stack of strings */ #include #include "stack.h" unsigned int main() { /* run the test */ push( " zebra.\n" ); push( " pet" ); push( " my" ); push( " like" ); push( "I" ); while( !empty() ) { printf( "%s", pop() ); } return 0; } xxxxxxxxxx cat > stack.c <<\xxxxxxxxxx /* stack.c -- implements one instance of a stack class */ #include #include #include "stack.h" 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; } xxxxxxxxxx cat > stack.h <<\xxxxxxxxxx /* stack.h -- interface to on instance of a stack class */ 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 */ xxxxxxxxxx cat > Makefile <<\xxxxxxxxxx # Makefile for dumb stack demo demo: main.o stack.o cc -o demo main.o stack.o main.o: main.c stack.h cc -c main.c stack.o: stack.c stack.h cc -c stack.c # make clean -- removes all files created by make clean: rm -f *.o rm -f demo rm -f shar xxxxxxxxxx