/*********************************************************************** * list_user.cpp * * A simple program to implement user commands for manipulating * a basic linked list * * Written by Paul Bonamy - 30 September 2009 * Re-written using pure C - 10 November 2009 ************************************************************************/ #include "linkedlist.h" #include #include /*********************************************************************** * 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 () { LLIST *head = 0; // we have to keep track of our own head pointer char cmd; // this will hold our user command int value; // user-supplied value to insert / remove / lookup char r; // return value from lookup check // scanf takes a format string and pointers to variables // which will receive the values read in from the terminal scanf("%c", &cmd); // read in an intial command while(isspace(cmd)) { scanf("%c", &cmd); } // cin >> cmd; // read in an intial command while(cmd != 'q') { // dump out if we get a q switch (cmd) { // figure out what we're doing case 'i': // insert into list scanf("%d", &value); // get the value to insert // cin >> value; list_insert(&head, value); // do the insert break; case 'd': // delete from list scanf("%d", &value); // get the value to delete // cin >> value; // get the value to delete list_remove(&head, value); // do the delete break; case 'e': // empty the list list_empty(&head); break; case 'p': // print the list list_print(head); break; case 'l': // look for value in list scanf("%d", &value); // get value to lookup r = list_lookup(head, value); // do lookup if (r) { printf("List has value %d\n", value); } else { printf("List doesn't have value %d\n", value); } 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); } } return 0; }