/* * testll.c Matt Bishop November 20, 1996 * * This program reads commands and integers from the standard input * and manipulates the linked list accordingly. * * Modification history: * 11/20/96 Matt Bishop original */ #include #include "llist.h" /* * macro */ #define SZ_BUF 1024 /* size of the input buffer */ /* * function prototypes */ int insert(LLIST **, int); /* insertion function */ int delete(LLIST **, int); /* deletion function */ void prforw(LLIST *); /* print function */ void prback(LLIST *); /* print in reverse function */ /* * This does the obvious -- read commands and add to or delete from * the list using the linked list functions */ void main(void) { LLIST *head = L_NULL; /* head of linked list */ char buf[SZ_BUF]; /* input buffer */ int sv; /* number of parts of comand read */ int i; /* number to operate on */ char c; /* the command */ /* * loop as long as the user throws input at us */ while(printf("> "), fgets(buf, SZ_BUF, stdin) != NULL){ /* * read command */ sv = sscanf(buf, "%c %d\n", &c, &i); /* * process the command */ switch(c){ case 'i': /* insert an element */ if (sv != 2) printf("need an integer; type 'h' for help\n"); else if (insert(&head, i) == 0) printf("insertion failed!\n"); break; case 'd': /* delete an element */ if (sv != 2) printf("need an integer; type 'h' for help\n"); else if (delete(&head, i) == 0) printf("%d not in list!\n", i); break; case 'h': /* help */ printf("Commands are:\n"); printf("\td delete from list\n"); printf("\th print help message\n"); printf("\ti insert into list\n"); printf("\tp print list in order\n"); printf("\tr print list in reverseorder\n"); printf("Command must be first char of input line\n"); break; case 'p': /* print */ (void) prforw(head); break; case 'r': /* print backwards */ (void) prback(head); break; default: /* ??? */ printf("unknown command '%c'\n", c); printf("for help type 'h'\n"); break; } } /* * say goodbye gracefully */ exit(0); }