Linked Lists in Java

Chapter 4

Reference variables

•      Draw diagrams to illustrate what is happening in  static and dynamic RAM as the following are executed.

•      Integer p;
Integer q;

•      p = new Integer(5);

•      p = new Integer(6);

•      q = p;

•      q = q+5;

•      q = new Integer(9);

•      p = null;

•      q = p;

 

Small quiz

•      Given these declarations, is the last statement true or false

•      Integer p;
Integer q;

•      p = new Integer(6);

•      q = new Integer(6);

•      p==q;  is this true or false??

•      If you want to compare the contents of p and q, you must use the equal method inherited from Object

•      p.equals(q)

 

Reference variables

•         Class Dog
   {  private String name;
       private int age;
   }

•      Write a constructor

•      The client of the class makes these statements:

     Dog myDog, yourDog;

     myDog = new Dog(Zelda, 6);

–    It is only when you call the constructor that the item is created

    yourDog = new Dog(Spot, 2);

    yourDog = myDog;

    myDog = null;

•      What is yourDog?

A class that references itself is often called a node

•       Class DogNode
{   private String name;
     private int  age;
     private DogNode  next
;     //nextDog is a reference variable

     }

•      Write the constructor

•       The client of the class DogNode makes these statements:

    DogNode  headDog; //    headDog is a reference variable

    headDog = new DogNode(Fido, 8);

    DogNode  myDog;  //myDog is just a reference variable, not a node

    myDog = new Dog2(Zelda, 6);  // now the node is created

    headDog.next = myDog; //headDog.next is a reference variable

 

•      What do the these nodes look like?

 

 

 

Linked lists

•       In this data structure, one of the instance variables in the class is a reference to an object of its own class

•       Example:  Class intNode
                {   int   info;        
// data can be any type
                    intNode      next;    }

•       Write a constructor.

•       Several objects of this class can be instantiated and chained together by having the next reference of one intNode object refer to the other.

   intNode head = new intNode(8);

   head.next = new intNode(14);

•       What does head.next.next refer to?

•       What does head.info refer to?

•       The second object’s next reference can refer to a third Node object, and so on.

•       Note that we are referring to components of a class exactly the same way we always have

 

List of library books

•      Three classes

–    Library—the driver

–    bookList –the list itself

–    bookNode—the class with the info and link

 

To make a linked list of names (Strings)

1) Create a node class to hold the name and a reference to itself

•         This class is usually defined inside the list class (an inner class)

 2) Create a class that to hold the list

–        Only a reference to the first item in the list is needed

–        This is an instance variable in the class

–        Methods to manipulate the list

•        AddToFront,   AddToEnd, InsertAt

•        Remove

•        Find

•        toString

 3) Create a class to hold the driver to test the class

 

The List class and its inner class Node

 public class List  // Class to hold reference to first node in the list
{   
      private Node head;   
// instance variable    

      public List ( )  
// constructor; sets up an empty list
             {    head = null;   }

      
      public void addToEnd(String item)

             {……….}

      
//other methods go here

     
private class Node       // inner class
        {
         
private String item;       // instance variables
         
private Node next;

         
public Node(String i)    // constructor                                                            
             {       item = i;
                  next = null;  }
         }  
// end class Node

     } // end class List

 

Driver to use list

•      A main( ) method

•      Must declare a variable of type List; this will call the List constructor

•      Other statements will probably call the methods in the class list to manipulate the list

–   I.e. add to the list, delete from the list etc.

 

Writing one of the methods for the List class

•      Add to the front of the list.

•      What are the preconditions?

–    The list must exist

–    It is either empty, or it has some items in it; we must consider both cases.

•      What do we need to do?

–    Figure this out first with diagrams

–    Draw the case where the list is empty and add a node

–    Draw the case where the list is not empty, and add a node

 

AddToEnd(String itemToAdd)

•      Declare:

–    1) a new node and put itemToAdd in it, and

–    2) an extra reference variable to find the end of the list

•       Check to see if the list is empty; if it is set head to point to the new node

•       If not empty, find the end of the list, then add the new node there

•       void addToEnd(String itemToAdd)

{   Node temp = new Node(itemToAdd);

     Node curr;  // needed to traverse the list

     if (head == null)   // if the list is empty

             head = temp;

     else   //the list is not empty
  {    curr = head;    
// set curr to the beginning of the list
        while (curr.next != null)  // while not at the end of the list

                      curr = curr.next;     
        curr.next = temp;   }

}  // end addToEnd