# You may have to edit this file to delete header lines produced by # mailers or news systems from this file (all lines before these shell # comments you are currently reading). # Shell archive made by dwjones on Fri Oct 27 10:47:57 AM CDT 2023 # To install this software on a UNIX system: # 1) create a directory (e.g. with the shell command mkdir stuff) # 2) change to that directory (e.g. with the command cd stuff), # 3) direct the remainder of this text to sh (e.g. sh < ../savedmail). # 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). Then read README # in the new directory for additional instructions. cat > main.c <<\xxxxxxxxxx /* main.c -- test my class implementing stack of strings */ #include #include "stack.h" unsigned int main() { stackhandle mystack = newstack(); push( mystack, " zebra.\n" ); push( mystack, " pet" ); /* logically, this is mystack.push( " pet" ) */ push( mystack, " my" ); push( mystack, " like" ); push( mystack, "I" ); while( !empty( mystack ) ) { /* logically, this uses !mystack.empty() */ printf( "%s", pop( mystack ) ); } return 0; } xxxxxxxxxx cat > stack.c <<\xxxxxxxxxx /* stack.c -- implements a non-polymorphic stack class */ #include #include #include "stack.h" /* private definition of a stack element */ struct stackelement { const char * str; /* the string in this stack element */ struct stackelement * next; /* link to rest of stack */ }; /* details of a stack object */ struct stack { struct stackelement * top; }; /* struct stack * stackhandle; this definition is given in stack.h */ stackhandle newstack() { stackhandle s = malloc( sizeof( struct stack ) ); s->top = NULL; } void push( stackhandle s, const char * str ) { /* push str on stack s */ struct stackelement * new = malloc( sizeof( struct stackelement ) ); new->str = str; new->next = s->top; s->top = new; } const char * pop( stackhandle s ) { /* return the top of stack s and pop */ const char * ret = s->top->str; struct stackelement * old = s->top; s->top = s->top->next; free( old ); return ret; } int empty( stackhandle s ) { /* reports 1 if stack empty, else 0 */ return s->top == NULL; } xxxxxxxxxx cat > stack.h <<\xxxxxxxxxx /* stack.h -- interface to a non-polymorphic stack class */ struct stack; /* details given and visible only in stack.c */ typedef struct stack * stackhandle; /* all stack users use these */ stackhandle newstack(); /* constructor for stack objects */ void push( stackhandle s, const char * str ); /* push str on stack s */ const char * pop( stackhandle s ); /* return the top of stack s and pops it */ int empty( stackhandle s ); /* 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