/* A program shell to maintain a linked list of names */
/* author:  Henry M. Walker */
/* updater: Jerod Weinman */ 

#include "node.h"
#include "list-proc.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>


/* Print menu options to the screen */
void
printMenu (void)
{
  printf ("Options available\n");
  printf ("I - Insert a name into the list\n");
  printf ("D - Delete a name from the list\n");
  printf ("C - Count the number of items on the list\n");
  printf ("F - Move an item to the front of the list\n");
  printf ("L - Print the last item on the list (iteratively)\n");
  printf ("P - Print the names on the list (iteratively)\n");
  printf ("R - Print the names in reverse order\n");
  printf ("Q - Quit\n");
} // printMenu

/* program to coordinate the menu options and call the requested function */
int
main (void)
{
  struct node * first = NULL;   /* pointer to the first list item */
  char option[1+1];       /* user response to menu selection */

  printf ("Program to Maintain a List of Names\n");

  while (1) {
    
    do { /* User input loop */
      printMenu(); /* print menu options */
      printf ("Enter desired option: "); /* determine user selection */
      if (scanf ("%1s", option)) /* read a single character as a string */
        break;
      else
        printf ("Error reading input. Try again.");
    } while (1);

    while (getchar() != '\n') /* Clear input line */
      ;
    
    switch (tolower(option[0])) {
      case 'i': addName(&first);
        break;
      case 'd': deleteName(&first);
        break;
      case 'c': countList(first);
        break;
      case 'f': putFirst(&first);
        break;
      case 'l': printLast(first);
        break;
      case 'p': print(first);
        break;
      case 'r': printReverse(first);
        break;
      case 'q':
        printf("Program terminated\n");
        return 0;
        break;
      default: printf("Invalid Option - Try Again!\n");
         continue;
    } // switch
  } // while
} // main
