stack/stackdemo.h :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /********************************************************************* * FILE: stackdemo.h * AUTHOR: Mike McCarthy * HARVARD ID: 00161118 * DATE: June 2003 * PURPOSE: header file for stackdemo.c *********************************************************************/ #define BUFSIZE 512 #define SMBUFSIZE 255 FILE* setup(char*); void fatal(char*, char*); [END: stack/stackdemo.h]======================================================== ================================================================================ ================================================================================ stack/stackdemo.c :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /********************************************************************* * FILE: stackdemo.c * AUTHOR: Mike McCarthy * HARVARD ID: 00161118 * DATE: June 2003 * PURPOSE: Demo of dynamic stack data structure. * * NOTES: Uses a simple struct containing a char* and int. * * Reads from filename supplied as command line arg. * * USAGE: At the command line, type: stackdemo data * *********************************************************************/ #include #include #include #include "stackdemo.h" #include "stack.h" int main(int argc, char* argv[]) { char buf[BUFSIZE], string[SMBUFSIZE], value[SMBUFSIZE]; node* top_node; FILE* fp = NULL; if(argc != 2) fatal("incorrect number of arguments", ""); fp = setup(argv[1]); printf("\n"); while(fgets(buf, BUFSIZE, fp) != NULL){ if(sscanf(buf, "%s%s", string, value) != 2) fatal("file is corrupt", ""); if(push(string, value) != OK) fatal("memory failure", ""); } printf("\n"); while((top_node = pop()) != NULL){ printf("popping: %s %d\n", top_node->str, top_node->num); delete_node(top_node); show_stack(); } fclose(fp); printf("\n"); return 0; } /********************************************************************/ FILE* setup(char* fname) { FILE* fp; fp = fopen(fname, "r"); if(!fp){ perror(fname); exit(1); } init_stack(); return fp; } /********************************************************************/ void fatal(char* s1, char* s2) { fprintf(stderr, "Error: %s %s\n", s1, s2); exit(1); } /********************************************************************/ [END: stack/stackdemo.c]======================================================== ================================================================================ ================================================================================ stack/stack.h :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /********************************************************************* * FILE: stack.h * AUTHOR: Mike McCarthy * HARVARD ID: 00161118 * DATE: June 2003 * PURPOSE: header file for stack.c *********************************************************************/ #define NOT_OK 0 #define OK 1 struct node{ char* str; int num; struct node* next; }; typedef struct node node; void init_stack(); int push(char*, char*); node* pop(); node* get_node(char*); void delete_node(node*); void show_stack(); [END: stack/stack.h]============================================================ ================================================================================ ================================================================================ stack/stack.c :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: /********************************************************************* * FILE: stack.c * AUTHOR: Mike McCarthy * HARVARD ID: 00161118 * DATE: June 2003 * PURPOSE: Implentation file for stack functions *********************************************************************/ #include #include #include #include "stackdemo.h" #include "stack.h" static node* top; /********************************************************************/ void init_stack() { top = NULL; } /********************************************************************/ int push(char* s1, char* s2) { node* new_node, *temp; new_node = get_node(s1); if(new_node == NULL) return NOT_OK; strcpy(new_node->str, s1); new_node->num = atoi(s2); printf("pushing: %s %d\n", new_node->str, new_node->num); if(top == NULL){ top = new_node; new_node->next = NULL; } else{ temp = top; top = new_node; new_node->next = temp; } show_stack(); return OK; } /********************************************************************/ node* get_node(char* str) { node* new_node = malloc(sizeof(node)); if(!new_node) return NULL; new_node->str = malloc(strlen(str) + 1); if(!new_node->str) return NULL; return new_node; } /********************************************************************/ node* pop() { node* popped_top; if(top == NULL) return NULL; popped_top = top; top = top->next; return popped_top; } /********************************************************************/ void delete_node(node* node_to_delete) { free(node_to_delete->str); free(node_to_delete); } /********************************************************************/ void show_stack() { node* temp; temp = top; printf("\tSTACK CONTAINS: "); while(temp != NULL){ printf("%s ", temp->str); temp = temp->next; } printf("\n"); } /********************************************************************/ [END: stack/stack.c]============================================================ ================================================================================ ================================================================================