00001 package plp.imperative1.util; 00002 00003 00004 public class Lista<T> { 00005 protected T head; 00006 protected Lista<T> tail; 00007 00008 00009 public Lista(){ 00010 head = null; 00011 tail = null; 00012 } 00013 00014 00015 public Lista(T valor, Lista<T> lista){ 00016 this.head = valor; 00017 this.tail = lista; 00018 } 00019 00020 00021 public int length() { 00022 00023 if ( head == null) { 00024 return 0; 00025 }else if (tail == null) { 00026 return 1; 00027 } else { 00028 return 1 + tail.length(); 00029 } 00030 00031 } 00032 00033 public T getHead() { 00034 return head; 00035 } 00036 00037 public Lista<T> getTail() { 00038 return tail; 00039 } 00040 00041 public String toString(){ 00042 StringBuffer resposta = new StringBuffer(); 00043 resposta = this.getString(resposta); 00044 return resposta.toString(); 00045 } 00046 00047 private StringBuffer getString(StringBuffer resposta){ 00048 if(head != null) { 00049 if ( tail != null) { 00050 resposta = tail.getString(resposta); 00051 } 00052 resposta.append(head.toString()+" "); 00053 } 00054 return resposta; 00055 } 00056 00057 }