Back to slide -- Keyboard shortcut: 'u'        next  LinkedList.java - Klassen LinkedList med pakke-synlig Linkable som inner class.Lecture 5 - slide 27 : 28
Program 1

/** A simple linked list with inner Linkable class which reveals the Linkable object in a unsatisfactory manner */

public class LinkedList {
  private Linkable firstLinkable;
  private int length;

  class Linkable {
    private Object data;
    private Linkable next;
  
    /** Return a Linkable object with null references in both data and next */ 
    Linkable(){
      data = null;
      next = null;
    }
  
    /** Return a Linkable object with null references in next, and the parameter as data */   
    Linkable(Object data){
      this.data = data;
      this.next = null;
    }
  
    /** Return a Linkable object with first parameter as data and second parameter as next */
    Linkable(Object data, Linkable next){
      this.data = data;
      this.next = next;
    }
  
    /** Return the data of this Linkable object */
    Object data(){
      return (data);
    }
  
    /** Return the reference to the next Linkable */
    Linkable next(){
      return (next);
    }
  
    /** Set the data field of this Linkable object */
    void setData(Object data){
      this.data = data;
    }
  
    /** Set the next field of this Linkable object */
    void setNext(Linkable next){
      this.next = next;
    }
  } // Linkable

  public LinkedList(){
    // make empty list
    firstLinkable = null;
    length = 0;
  }

  public void insertFirst(Object element){
    Linkable newFirst = new Linkable(element, firstLinkable);
    firstLinkable = newFirst;
    length = length + 1;
  }

  public void deleteFirst(){
    if (length > 0){
      firstLinkable = firstLinkable.next();
      length = length - 1;
    }
  }

  public Linkable firstLinkable(){
    return firstLinkable;
  }

  public int length(){
    return length;
  }
} // LinkedList