/*********************************************************************** * prog2.cpp * * A simple program to implement user commands for manipulating * an expandable queue. * * Written by Paul Bonamy - 23 May 2010 * Rewritten in pure C - 20 July 2010 ************************************************************************/ #include // replaces iostream for I/O #include // malloc lives here #include // we get isspace() from here #include "equeue.h" /*********************************************************************** * int main() * * main function for the program. deals with accepting/parsing * user input and generally keeping things running * * no parameters / always returns 0 ***********************************************************************/ int main () { EQUEUE *queue; // our queue char cmd; // this will hold our user command int value; // user-supplied value to insert / remove queue = (EQUEUE *)malloc(sizeof(EQUEUE)); equeue_init(queue); // initialize the queue structure scanf("%c", &cmd); // read in an intial command /* * scanf will return whatever character it finds, so we need some way * to weed out the whitespaces while looking for characters */ while(isspace(cmd)) { scanf("%c", &cmd); } while(cmd != 'q') { // dump out if we get a q switch (cmd) { // figure out what we're doing case 'i': // insert into queue scanf("%d", &value); // get the value to insert equeue_enqueue(queue, value); // do the insert break; case 'e': // empty the queue equeue_empty(queue); break; case 'r': // dequeue and print last value if(equeue_dequeue(queue, &value)) { printf("%d\n", value); } else { printf("empty\n"); } break; default: // here for giggles printf("Oi! That's not a legal command\n"); } scanf("%c", &cmd); // get the next command while(isspace(cmd)) { scanf("%c", &cmd); } } equeue_cleanup(queue); free(queue); return 0; }