Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. //Klasse für Nodes
  2. public class MyNode <T>{
  3.     MyNode next;
  4.     T data;
  5.    
  6.     public MyNode(T in)
  7.     {
  8.         next = null;
  9.         this.data = in;
  10.     }
  11.    
  12.     public void Print()
  13.     {
  14.         System.out.println(this.data.toString());
  15.     }
  16.    
  17.     public MyNode getNext()
  18.     {
  19.         return this.next;
  20.     }
  21.    
  22.     public void setNext(MyNode n)
  23.     {
  24.         this.next = n;
  25.     }
  26.  
  27. }
  28.  
  29. //Klasse für die Liste
  30.  
  31. public class MyList {
  32.     MyNode first;  
  33.    
  34.     public MyList()
  35.     {
  36.         first=null;
  37.     }
  38.  
  39.     public void add(MyNode n)
  40.     {
  41.         if (first==null) this.first = n;
  42.         else
  43.         {
  44.             MyNode it;
  45.             for( it = this.first; it.getNext()!=null; it=it.getNext());
  46.             it.setNext(n); 
  47.             }
  48.        
  49.     }
  50.     public void printAll()
  51.     {
  52.         if (first==null) System.out.println("Keine Nodes!");
  53.         else
  54.         {
  55.             MyNode it;
  56.             for( it = this.first; it!=null; it=it.getNext())
  57.             {
  58.                 it.Print();
  59.             }
  60.         }
  61.     }
  62.    
  63.     public void reverse() // rekursiv! noch überarbeiten Q_Q
  64.     {
  65.         if (first==null) return;
  66.         else
  67.         {
  68.             MyNode it;
  69.             MyNode oldfirst = new MyNode(0);
  70.             MyNode newfirst = new MyNode(0);
  71.             for( it = this.first; it!=null; it=it.getNext())
  72.             {
  73.                 this.first = newfirst;
  74.                 it = oldfirst;
  75.                 first.next = oldfirst;
  76.                
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. //Die Main
  83. public class Main {
  84.     public static void main(String[] args)
  85.     {
  86.        
  87.         MyList list = new MyList();
  88.        
  89.         list.add(new MyNode(3));
  90.         list.add(new MyNode("blafasel"));
  91.         list.add(new MyNode(true));
  92.        
  93.         list.printAll();
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement