lists :: list methods


 // adds node at the front of the list
  void addAtFront(int item)
  {
    head = new Node(item,head); 
  }

  void addAtRear(int item) 
  {   
   Node newNode = new Node(item,null);
    
    for(Node temp = head; temp!=null; temp = temp.link) // find last node
    {
      if(temp.link==null)//once last node is found
      {
        temp.link= newNode;//have the last node point to the new node
      }
    }
    
  }
  
  // prints contest of a Link List
  void printList()
  {
    // temp refers to the first node in the list
    // use 'temp =  temp.link;' to move from node to node

    for(Node temp = head; temp!=null; temp = temp.link)
    {
      System.out.println(temp.item); 
    }
  }

  void insert(int item)
  {
    //find location to insert
    Node current = head;
    Node prev = null;
    boolean located = false;
    
    while(!located && current != null)//correct location found
    { 
      if(item < current.item)
      {
        located = true; 
      }
      else 
      {
        //traverse list
        prev = current;
        current = current.link;
      }
    }//end of while
 
      //create new node and
      //refer to the correct location in list 
      Node newNode = new Node(item, current);
      
      //set link to refer to new node
      if (current == head)
      {
        head = newNode;//new node to head
      }
      else
      {
       //place new node after previous LINK reference not the node itself
       prev.link = newNode; 
      }    
    } 
  }

  void deleteFirst()
  {
    if (head == null)
      System.out.println("empty list");
    else
    head = head.link; 
  }
  
  void deleteLast()
  {
   if(head==null)
    {
      System.out.print("empty list ");
    }
    else
    {
      for(Node temp = head; temp!=null; temp = temp.link)
      {
        // if the node points to null then it is the last node
        if(temp.link==null)
        {
          temp=null; // last node points to null
        }
      }
    }
  }

  void delete (int item)
  {
    // search for node to be deleted   
    Node current = head;
    Node prev = null;
    boolean found = false;
    
    while(!found && current !=null)
    {
      if(current.item == item)
      {
        found=true;
      }
      else
      {
        prev = current;
        current =  current.link;
      }
    }//end of while
 
      //if found, perform  deletion      
      if(found) //true when the previous if sets 'found' to true
      {
        if(current == head)
        {
          head = head.link;//second link becomes head
        }
        else
        {
          //previous will point to where current was pointing at
          prev.link = current.link;
        }
      }
    }

  // add all int values in list
  int sumList ()
  {
    int accm= 0;
    
    for(Node temp = head; temp!=null; temp = temp.link)
    {
      accm+= temp.item;
    }  
    return accm;
  }
}