// Program Name         IntList.java
// Course:              CSE 1302
// Student Name:        Bradley Shedd
// Assignment Number:   Project#5 ExtraCredit
// Due Date:            12/06/2010
// Purpose:             Defines a class that represents a list of integers    
// ****************************************************************
   public class IntList
   {
     
private IntNode front;      //first node in list
  
   
//-----------------------------------------
    //  Constructor.  Initially list is empty.
    //-----------------------------------------
      public IntList()
      {
         front =
null;
      }
  
//A delete method for linked lists: (can be used for extra credit)
      public void delete (int item)
      { IntNode current = front;
        
if (current == null)
           
return;
        
if (item == current.val)
         { front = front.next;}
        
        
else {   
        
           
while (current.next != null && current.next.val != item)
               current = current.next;
           
if (current.next.val == item)
               current.next = current.next.next;
           
else
               System.out.println("" + item + " not found in front");
         }
      }
  
//An insertInOrder method for linked fronts: (can be used for extra credit)
      public void insertInOrder (int n)
      { IntNode newnode =
new IntNode (n, null);
        
if (front == null)
            front = newnode;
        
else
         { IntNode temp = front;
           
if (temp.val >= newnode.val)
            {newnode.next = front;
               front = newnode;
            }
           
else
            {
              
while (temp.next != null && temp.next.val < newnode.val)
                  temp = temp.next;
               newnode.next = temp.next;
               temp.next = newnode;
            }
         }
      }
  
   
//-----------------------------------------
    //   Adds given integer to front of list.
    //-----------------------------------------
      public void addToFront(int val)
      {
         front =
new IntNode(val,front);
      }
  
   
//--------------------------------------
    //   Adds given integer to end of list.
    //--------------------------------------
      public void addToEnd(int val)
      {
         IntNode newnode =
new IntNode(val,null);
     
     
//if list is empty, this will be the only node in it
         if (front == null)
            front = newnode;
        
else
         {
        
//make temp point to last thing in list
            IntNode temp = front;
           
while (temp.next != null)
               temp = temp.next;
        
//link new node into list
            temp.next = newnode;
         }
      }
  
   
//-------------------------------------------
    //   Removes the first node from the list.
    //   If the list is empty, does nothing.
    //-------------------------------------------
      public void removeFirst()
      {
        
if (front != null)
            front = front.next;
      }
  
   
//------------------------------------------------
    //   Prints the list elements from first to last.
    //------------------------------------------------
      public void print()
      {
         System.out.println(
"---------------------");
         System.out.print(
"List elements: ");
     
        IntNode temp = front;
     
        
while (temp != null)
         {
            System.out.print(temp.val +
" ");
            temp = temp.next;
         }
         System.out.println(
"\n---------------------\n");
      }
  
     
public int length()
      {
         IntNode temp = front;
        
int count = 0;
        
while(temp != null)
         {
            count++;
            temp = temp.next;
         }
        
return count;
      }
  
     
public String toString()
      {
         String report =
"";
         IntNode temp = front;
        
while(temp != null)
         {
            report += temp.val +
"\n";
            temp = temp.next;
         }
        
return report;
      }
  
     
public void removeLast()
      {
        
if(front == null)
           
return;
        
if(front.next == null)
            front =
null;
        
else 
         {IntNode current = front;
           
while (current.next.next != null)
            {
              current=current.next;
            }
            current.next =
null;
         }
      }
  
     
public void replace(int oldVal, int newVal)
      {
     
         IntNode temp = front;
        
while(temp != null)
         {
           
if(temp.val == oldVal)
               temp.val = newVal;
           temp = temp.next;
         }
     
      }
     
public void printRec()
      {
         IntNode temp = front;
         printRecHelp(temp);
      }
  
     
public static void printRecHelp(IntNode f)
      {
         
if (f!=null)
         {
            System.out.println(f.val);
            printRecHelp(f.next);
         }
      }
  
    
public void printRecBkw()
      {
         System.out.println();
         IntNode temp = front;
         printRecBkwHelp(temp);
      }
  
     
public static void printRecBkwHelp(IntNode b)
      {
        
if (b!=null)
         {
            printRecBkwHelp(b.next);
            System.out.println(b.val);
         }
      }
  
  
   
//***************************************************************
    // An inner class that represents a node in the integer list.
    // The public variables are accessed by the IntList class.
    //***************************************************************
      private class IntNode
      {
        
public int val;         //value stored in node
         public IntNode next;    //link to next node in list
     
     
//-------------------------------------------------------------------
      // Constructor; sets up the node given a value and IntNode reference
      //-------------------------------------------------------------------
         public IntNode(int val, IntNode next)
         {
           
this.val = val;
           
this.next = next;
         }
      }
   }

Homepage