/** 
    FILE:           Lists.java
    AUTHOR:         Cheng, Jeff
    LAST CHANGE:    3-22-02
    PURPOSE:        The lists package permits searching in either a doubly
                    linked list or a skip list.
                    BONUS:  print a doubly linked list in reverse order
                            recursively, without the use of system stack.
    
    COPYRIGHT:      You may use, modify, or distribute my code for 
                    non-commerical use only; you must cite me, Jeff Cheng, 
                    as the source. 
*/

package lists;

import javax.swing.*;

public class Lists{
    public static void main(String args[]){
        boolean again = false;
        do{
            System.out.println("\n\t This program performs the following" +
                                " tasks:\n\t Choose 0 to print a linked" + 
                                " list in reverse using recursion.\n\t" +
                                " Choose 1 to create and search items in" + 
                                "a skip list.\n\t Choose 2 to create and" + 
                                "search items in a linked list.\n\t" +
                                " All other input terminates the program.");

            String userChoice = JOptionPane.showInputDialog
                                ("\t What would you like to do?");
        
            int chosen = 0;
            
            try{
                chosen = Integer.parseInt(userChoice);                            
            }catch(NumberFormatException e){
                again = false;
            };

            boolean sizeIsNumber = true;
            boolean numItemsIsNumber = true;   //  number of items to find
            String numItems = "";
            String size = "";
            int listSize = 0;
            int itemsToFind = 0;
                                    
            switch(chosen){
                case 0:  //  print list in reverse by recursion.
                LinkedList aLinkedList = new LinkedList();
                
                do{
                    sizeIsNumber = true;                        
                    size = JOptionPane.showInputDialog
                             ("\t What list size would you like?" +
                              "\t List size must be a" +
                              " non-negative number.");
                
                    try{
                        listSize = Integer.parseInt(size);                                                  
                    }catch (NumberFormatException e){
                        sizeIsNumber = false;}
                }while ((!sizeIsNumber)||(listSize<0));
                
                for (int i = 0; i < listSize; i++){ 
                    aLinkedList.insert(new Nodes(i*i));
                    }
                System.out.println("Original list:");
                aLinkedList.printList();
                System.out.println("Now print in reverse:");
                
                aLinkedList.printInReverse(LinkedList.getHead());
                
                again = true;
                break;
        
                case 1: //  create a skip list and search for items
                do{
                    sizeIsNumber = true;                        
                    size = JOptionPane.showInputDialog
                               ("\t What list size would you like?" +
                                "\t List size must be a" + 
                                " non-negative number.");
                
                    try{
                        listSize = Integer.parseInt(size);                                                  
                        }catch (NumberFormatException e){
                            sizeIsNumber = false;}
                    }while ((!sizeIsNumber)||(listSize < 0));

                do{
                    numItemsIsNumber = true;                        
                    numItems = JOptionPane.showInputDialog
                                ("\t How many items to search?" +
                                 "\t Number of items to search" +
                                 " must be a non-negative number.");
                
                    try{
                        itemsToFind = Integer.parseInt(numItems);                                                  
                        }catch (NumberFormatException e){
                            numItemsIsNumber = false;}
                    }while ((!numItemsIsNumber)||(itemsToFind < 0));

                SkipList.createAndSearch(listSize,itemsToFind);
                
                again = true;
                break;
                
                case 2:
                //  create a linked list and search for items
                do{
                    sizeIsNumber = true;                        
                    size = JOptionPane.showInputDialog
                            ("\t What list size would you like?" +
                             "\t List size must be a" +
                             "non-negative number.");
                
                    try{
                        listSize = Integer.parseInt(size);                                                  
                        }catch (NumberFormatException e){
                            sizeIsNumber = false;}
                    }while ((!sizeIsNumber)||(listSize < 0));

                do{
                    numItemsIsNumber = true;                        
                    numItems = JOptionPane.showInputDialog
                                 ("\t How many items to search?" +
                                  "\t Number of items to search" +
                                  " must be a non-negative number.");
                
                    try{
                        itemsToFind = Integer.parseInt(numItems);                                                  
                        }catch (NumberFormatException e){
                            numItemsIsNumber = false;}
                    }while ((!numItemsIsNumber)||(itemsToFind < 0));
                    
                LinkedList.createAndSearch(listSize, itemsToFind);
                
                again = true;
                break;
                
                default:
                System.out.println("\t You chose to quit.  Thank you!");
                again = false;
                break;
                }   //  end of switch
            } while (again);    //  end of do-while
        
    System.exit(0);
    }
}