Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: None  |  size: 0.69 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class Listb implements List{
  2.  Object element;
  3.  Listb tail;
  4.  
  5.  public Listb() {
  6.   element = null;
  7.   tail = null;
  8.  }
  9.  
  10.  private Listb(Object o, Listb t) {
  11.   element = o;
  12.   tail = t;
  13.  }
  14.  
  15.  public boolean isEmpty() {
  16.   return element == null && tail == null;
  17.  }
  18.  
  19.  public Object head() {
  20.   return element;
  21.  }
  22.  
  23.  public List tail() {
  24.   return tail;
  25.  }
  26.  
  27.  public List cons(Object o) {
  28.   Listb newList = new Listb(o, this);
  29.   return newList;
  30.  }
  31.  
  32.  public int len() {
  33.   int i = 0;
  34.   Listb lp = this;
  35.   while (!lp.isEmpty()) {
  36.    i++;
  37.    lp = lp.tail;
  38.  
  39.   }
  40.   return i;
  41.  }
  42.  
  43.  public int rlen() {
  44.   if (isEmpty())
  45.    return 0;
  46.   else
  47.    return 1 + tail.rlen();
  48.  }
  49.  
  50. }