// cs 102
// lab 6 code

#include <iostream.h>
#include <ctype.h>
#include <assert.h>

// uses some Ford & Topp list code
#include "lab6listOps.h"

// BUGGY sumList
// PRE: list is a well-formed list
// POST: returns sum of elements in list
// NOTE: only works if ElmtType is a type for which we can do +=)
ElmtType sumList (Node *list)
{
  Node *p = list;
  int sum = 0;

  while (p!=NULL) {
    sum += p->data;
    p->next = p;
  }
  return sum;
}

// BUGGY append routine
// PRE: list is a well-formed list
// POST: list' is same as list, but with item appended
void append (Node * &list, ElmtType item)
{
  Node *p = list;

  if (p == NULL) {
    insertFront(list, item);
    return;
  }

  while (p->next != NULL) {
    p = p->next;
  }

  p = getNode(item);

}

/* 
 * BUGGY deleteKth
 * PRE: list is a well-formed list
 * POST: returns a list the same as list, but with the Kth element deleted.
 *  list' is undefined.
 *  If (k < 1 or k > (length of list)), returns list
 */
Node * deleteKth (Node * &list, int k)
{
  int i;
  Node * p = list;

  if ((list == NULL) || (k < 1)) {
    return list;
  }

  if (k == 1) {
    p = list->next;
    delete list;
    return p;
  }

  for (i = 0; p != NULL && i < k; i++) {
    p = p->next;
  }

  if (p != NULL) {
    p->deleteAfter();
  }

  return list;
}


// printEveryOther: prints 1st, 3rd, 5th, ... elements in a list
// PRE: list is a well-formed linked list
void printEveryOther(Node * list)
{
  // only partially written -- you must complete
  // REMOVE THE FOLLOWING TWO LINES
  cout << "printEveryOther has not been implemented yet." << endl;
  return;   // REMOVE THIS LINE

  while (list != NULL) {
    cout << list->data << " ";

    // YOU COMPLETE THE BODY OF THE LOOP
  }
  cout << endl;

}


/*
 * promptForInt: Prompts for and reads an integer.
 * PRE: prompt must be a well-formed C string
 */
int promptForInt (char *prompt)
{
    int value;

    cout << prompt << ": ";
    cin >> value;
    return value;
}

// skips over non-digits, non eoln in input stream
void skip()
{
  int ch = cin.peek();
  while ((ch != '\n') && (!isdigit(ch))) {
    cin.get();
    ch = cin.peek();
  }  
}

// buildList: reads numbers from one line, prepends them to the list
// in reverse order.
// User must enter at least one number, and terminate sequence of
// numbers with a newline.
// PRE: theList is a well-formed list
void buildList(Node * & theList)
{
  int i;

  do {
    cin >> i;
    insertFront(theList, i);
    skip();
  } while (cin.peek() != '\n');
  cin.get();     // consume the newline
}

/* a little test program */

main ()
{

  char c;
  int keepgoing = 1;
  int  v, k;
  Node *thelist = NULL;

  cout << "Enter 'h' for a description of the commands." << endl;

  do {
    cout << "\nPlease enter a command [b, i, a, d, s, p, q]: ";
    cin >> c;

    switch (c) {
    case 'b':
      clearList(thelist);
      cout << "Please enter a sequence of ints followed by <nl> (e.g:1 2 3): ";
      buildList(thelist);
      break;
    case 'i':
      v = promptForInt ("Value to insert");
      insertFront (thelist, v);
      break;
    case 'a':
      v = promptForInt ("Value to append");
      append (thelist, v);
      break;
    case 'd':
      k = promptForInt ("Position of element to delete");
      thelist = deleteKth (thelist, k);
      break;
    case 's':
      cout << "Sum of elements in list is " << sumList(thelist) << endl;
      break;
    case 'p':
      cout << "Print every other: " << endl;
      printEveryOther(thelist); cout << endl;
      break;
    case 'q':
      keepgoing = 0;
      break;
    default:
      cout<< "Valid commands are b(build new list), i(insert), a(append), d(delete)" << endl;
      cout << "         s(sum of elmts) , p(print every other), q(quit)." << endl;
      break;
    }
    
    cout << "The list: ";
    printList (thelist);  cout << endl;
    
  } while (keepgoing);
}
