/*************************************************************\
This is a bad example becasue the beginning and end of loops and function are not aligned.
The comments are place at the begining of the line.  The case statment is spaced and organized
poorly, and the data declarations are also unaligned.
This makes the program difficult to read.
\*************************************************************/

#include <iostream.h>
#include "linklist.cpp"
#include "myerror.cpp"

//Menu option
int menu()
{
    int choice;
// Display menu on scream
    cout << endl;
    cout << "(1) Add several values to the list." << endl;
    cout << "(2) Remove a give values." << endl;
    cout << "(3) Add a values at a give position in the list." << endl;
    cout << "(4) Display the list." << endl;
    cout << "(5) Delete the list." << endl;
    cout << "(6) Exit." << endl;
    cout << endl;

// Get user input the choice in the menu,
    cout << "Enter your choice (1-6): ";
    cin >> choice;
    cout << endl;

// Check if the input is valid.
    while (choice > 6 || choice < 1)
    {

// Display error message
    cout << "Invalid choice!" << endl;
    cout << "Please enter your choice again (1-6): ";
    cin >> choice;
    cout << endl;
    }

    return choice;
}

void main()
{
    LinkedList* list = new LinkedList();
    const ListElement* ele;

    int choice, i, position, value, max, secmax, num;
    position = 0;
    choice = menu();

    while (choice < 10)
    {
      switch (choice)
      {
      case 1 : cout << "Please enter the value to list, exit(-1): ";
      cin >> value;
      while (value > 0)
      {
      list->Append(value);
      cout << "Please enter the value to list, exit(-1): ";
      cin >> value;
      }
      cout << endl;break;

      case 2 : cout << "Please enter the value to remove from the list: ";
      cin >> value;
      list->Extract(value);
      cout << endl; break;

      case 3 : cout << "Please enter the values: ";
      cin >> value;
      cout << "Please given position in the list: ";
      cin >> position;
      ele = list->Head();
      num = position - 2;
      for (i = 0; i < num; ++i)
      { ele = ele->Next(); }

      list->InsertAfter(ele,value);
      cout << endl;
      break;

      case 4 : ele = list->Head();
      if (ele == 0)
      { cout<<"Thi list is empty"<<endl; }
      while (ele != 0)
      {
      cout<<ele->Datum()<<endl;
      ele = ele->Next();
      }
      cout << endl; break;

      case 5 : cout << "Delete the list." << endl;
      list->Purge();
      cout << endl; break;

      default: cout << "Invalid choice" << endl;
      cout << endl;
      }
     choice = menu();
    }

    cout << endl;
    cout << "Exit the program." << endl;
    cout << endl;
    cout << "Thank You!!!!!!!!!!!!!" << endl;
}