import ListNode;

/**
* Project3, due: 5.10.2001 <br>
* LinkedList Creates a LinkedList that the user can manipulate and search through.
* @author Group1
*/

public class LinkedList {

/**
* firstNode the ListNode containing the firstNode of the LinkedList.
*/
        ListNode firstNode;

/**
* will keep track of the size of the node
*/
    int length;

/**
*variable to keep track of the deleted last record
*/
    String DLast;
/**
* LinkedList creates an empty LinkedList.
*/
    public LinkedList() {
    } // end constructor

/**
* LinkedList creates a LinkedList with a firstNode.
* @param firstNode the value for the LinkedList's first Node.
*/
    public LinkedList(ListNode firstNode) {

                this.firstNode = firstNode;
                firstNode.link = null;
                length = 1;
        } // end constructor
/**
 * insert a first node of list.
 * This method was written by Minh Bui for Group1, Project 3.
 * @param if the list was empty the first node will insert.
 * @param declare a pointer N that can point to ListNode.
 * @param setthe airport of the N to the method's airportCode argument.
 * @param increase the length by one
 */
        public void insertFirstNode(String airportCode) {
                if (length == 0){
                        ListNode N = new ListNode();
                        N.airport = airportCode;
                        N.link = null;
                        firstNode = N;
                } //end if
        	
                else {
                        ListNode N = firstNode;
                        firstNode = new ListNode(airportCode);
                        firstNode.link = N;
                } // end else
        } // end insertFirstNode
	
/** 
 * delete a first node from a list
 * This method was written by Minh Bui for Group1, project 3.
 * @param set the first node empty and return firstNode.airport
 * @param else firstNode equal firstNode.link and decrease its length by one.
 */
	
        public String deleteFirstNode() {
        	
                String temp = firstNode.airport;
        	
                if (firstNode == null)
                {
                        return ("The list is empty");
            }
                firstNode = firstNode.link;
                return temp;
        } // end deleteFirstNode

    public void insertLastNode(String airportCode) {

    } // end insertLastNode

    public String deleteLastNode() {

        return DLast;
    } // end deleteLastNode
/**
*getSize is a number value, of airports inputed.
*This mehod was written by Bryan Carcelen,G1,P3.
*This method records a number value of the many airports that
*the user has entered.
*@return Integer value for a total amount of airports entered.
*if there are null airports entered, then the length is 0.
*/
    public int getSize() {

        ListNode N=firstNode;

        if(N==null){
            length=0;
            return length;
        }

        while(N==null){
            N=N.link;
        length++;
        }
        return length;

        }// end getSize

/**
*displayAll shows all airport codes in the list by the user input.
*This method checks if there are null airport entered,
*then message will display.
*Airport codes will display horizontally after every comma.
*/
        public void displayAll(){

                ListNode N = firstNode;

              // first, print a left parenthesis
                        System.out.print( "(" );

                if (N == null){
                        System.out.print("There are no entries in the list.");
                }//endif
                else{

       // provided N doesn't point to an empty node, print N's airport
       // and advance N to point to the next node on the list

                        while (N != null){
                System.out.print(N.airport + ", ");     // print airport code
                                N = N.link;                                                   // print comma between list items
            }//endwhile

        }//endelse
                 // finally, print a closing right parenthesis
                 System.out.println( ")" );
   }//end displayAll

/**
* doSearch Searches a Linked List for an airport.
* This method was written by Eric Berry for Group1, project3.
* @param node he starting ListNode from which the search will start.
* @param airportCode he string value of the Airport the user is searching for.
* @return String If the Airport was found, it returns the airportCode for that airport.
* otherwise it just returns an error message of what happened.
*/
        public String doSearch(ListNode node,String airportCode) {
        	
                if(node == null) {                          // if the node is null then
                                                        // the list must be empty
                return("List is Empty\n");
        } // end if

        if (node.airport.equals(airportCode) == true) {                                                                                     	
                System.out.println("They are equal"); // debug
                System.out.println(node.airport);       // debug
                return("Found: " + node.airport);
                } // end if
        	
        if (node.link != null) {              // if the node's link isn't null then
                                                                                        // this isn't the last node so search      
                        doSearch(node.link, airportCode);       // again with the link as the first node 
        } // end if

                                                                                                                                        // if it hasn't met the first 3 requirments
        return ("Could not find: " + airportCode);      // then the value isn't in the list.
        } // end doSearch                                                                                  
} // end LinkedList


