Advertisement
piffy

Lista lineare (base)

Dec 10th, 2013
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. /*
  2.  * Aggiungere a questa lista i seguenti metodi
  3.  * un metodo che stabilisce se la lista è ordinata
  4.  * un metodo che fonde una lista con un'altra
  5.  * Di entrambe calcolare la complessità  O(n)
  6.  *
  7.  */
  8. package liste;
  9.  
  10. /**
  11.  *
  12.  * @author m.missiroli
  13.  */
  14. public class Liste {
  15.  
  16.     private String info;
  17.     private Liste next;
  18.    
  19.     public Liste() {
  20.     info=null;
  21.     next=null;
  22.     }
  23.  
  24.     public String getInfo() {
  25.         return info;
  26.     }
  27.  
  28.     public void setInfo(String info) {
  29.         this.info = info;
  30.     }
  31.  
  32.     public Liste getNext() {
  33.         return next;
  34.     }
  35.  
  36.     public void setNext(Liste next) {
  37.         this.next = next;
  38.     }
  39.    
  40.     public void addHead(String s)
  41.     {
  42.        Liste l = new Liste();
  43.        l.info=s;
  44.        l.next=this.next;
  45.        this.next=l;
  46.     }
  47.    
  48.     public String toString() {
  49.         String comodo="";
  50.         Liste l=this;
  51.     while(l.next!=null) {
  52.     comodo=comodo+l.next.info;
  53.     /** vai avanti **/
  54.     l=l.next;
  55.     }
  56.     return comodo;
  57.     }
  58.     public String toStringRec(Liste l) {
  59.     if (l.next==null) return "";
  60.     return this.next.info+toStringRec(l.next);
  61.     }    
  62.    
  63.    
  64.     /**
  65.      * @param args the command line arguments
  66.      */
  67.     public static void main(String[] args) {
  68.         // TODO code application logic here
  69.         Liste l= new Liste();
  70.         System.out.println(l);
  71.         l.addHead("Pippo");
  72.         System.out.println(l);
  73.         l.addHead("Pluto");
  74.         System.out.println(l);      
  75.        
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement