/*************************************************************\
This example is well orgainzed, the beginning and end of loops and function are aligned.
The comments are algin to the statements that they are describing.
Successive assignement operator are all aligned with the equal sign operators
The case statment is well space and organized and the data declarations are also well aligned.
\*************************************************************/

#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;
}

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

    int i
    int num;
    int choice;
    int position;
    int value;
    int max;
    int secmax;

    position = 0;
    choice   = menu();

    // Evaluate choice
    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();
    }

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