Ubgrade 11.1
							parent
							
								
									8a8fbc4e93
								
							
						
					
					
						commit
						a137bfc354
					
				|  | @ -5,31 +5,79 @@ | ||||||
|  * @author (Ihr Name)  |  * @author (Ihr Name)  | ||||||
|  * @version (eine Versionsnummer oder ein Datum) |  * @version (eine Versionsnummer oder ein Datum) | ||||||
|  */ |  */ | ||||||
| public class LinkedList | public class LinkedList<T> | ||||||
| { | { | ||||||
|     public Node first; |     public Node<T> first; | ||||||
|    public LinkedList(){ |     public LinkedList(){ | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     public void einfuegen (int neu) { |     public void einfuegen (T neu) { | ||||||
|     Node n = new Node(neu);//Neue Node mit Zahl "neu " anlegen
 |         Node<T> n = new Node<T>(neu);//Neue Node mit Zahl "neu " anlegen
 | ||||||
|      | 
 | ||||||
|     //Überprüfe ob die liste leer ist
 |         //Überprüfe ob die liste leer ist
 | ||||||
|     if(first == null){ |         if(first == null){ | ||||||
|         //setze neue node als erster Eintrag
 |             //setze neue node als erster Eintrag
 | ||||||
|         first = n; |             first = n; | ||||||
|  |         } | ||||||
|  |         else { | ||||||
|  |             Node<T> current = first; | ||||||
|  | 
 | ||||||
|  |             while(current.next != null){ | ||||||
|  |                 current = current.next; | ||||||
|  |             } | ||||||
|  |             current.setNext(n); | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         //current ist jetzt der letzte Eintrag
 | ||||||
|  |         //setze neue Node als Nachfolger von bisher letztem Eintrag
 | ||||||
|     } |     } | ||||||
|     else { | 
 | ||||||
|  |     public int laenge(){ | ||||||
|         Node current = first; |         Node current = first; | ||||||
|      |         int laenge = 0; | ||||||
|     while(current.next != null){ |         while(current != null){ | ||||||
|     current = current.next; |             current = current.next; | ||||||
|  |             laenge++; | ||||||
|  |         } | ||||||
|  |         return laenge;    | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
|     current.setNext(n); | 
 | ||||||
|  |     public T getNteZahl(int n){ | ||||||
|  |         Node<T> current = first; | ||||||
|  |         for(int i = 0; i<n; i++){ | ||||||
|  |             current = current.next;    | ||||||
|  |         } | ||||||
|  |         return current.zahl; | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
|     //current ist jetzt der letzte Eintrag
 |     public void loesche(int n){ | ||||||
|     //setze neue Node als Nachfolger von bisher letztem Eintrag
 |         Node<T> current = first; | ||||||
|  | 
 | ||||||
|  |         //gehe an den Vorgänger des zu löschenden Eintrags
 | ||||||
|  |         for(int i=0; i<n-1; i++){ | ||||||
|  |             current = current.next; | ||||||
|  |         } | ||||||
|  |         //setze Übernächsten Eintrag als Nachfolger
 | ||||||
|  |         current.next = current.next.next; | ||||||
|  | 
 | ||||||
|     } |     } | ||||||
|      | 
 | ||||||
| } |     public void hinzufügen(int n, T wert){ | ||||||
|  | 
 | ||||||
|  |         Node<T> neu = new Node<T>(wert); | ||||||
|  |         if(n==0){ | ||||||
|  |             neu.setNext(first); | ||||||
|  |             first = neu; | ||||||
|  |         }else{ | ||||||
|  |             Node<T> current = first; | ||||||
|  |             //gehe an den Vorgänger des zu löschenden Eintrags
 | ||||||
|  |             for(int i=0; i<n-1; i++){ | ||||||
|  |                 current = current.next; | ||||||
|  |             } | ||||||
|  |             //setze Übernächsten Eintrag als Nachfolger
 | ||||||
|  |             current.next = current.next.next; | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |     } | ||||||
|  | } | ||||||
|  | @ -5,14 +5,14 @@ | ||||||
|  * @author (Ihr Name)  |  * @author (Ihr Name)  | ||||||
|  * @version (eine Versionsnummer oder ein Datum) |  * @version (eine Versionsnummer oder ein Datum) | ||||||
|  */ |  */ | ||||||
| public class Node | public class Node<T> | ||||||
| { | { | ||||||
|      |      | ||||||
| public int zahl; | public T zahl; | ||||||
| 
 | 
 | ||||||
| public Node next; | public Node next; | ||||||
| 
 | 
 | ||||||
| public Node(int z){ | public Node(T z){ | ||||||
| zahl = z; | zahl = z; | ||||||
| } | } | ||||||
| public void setNext(Node n){ | public void setNext(Node n){ | ||||||
|  |  | ||||||
							
								
								
									
										15
									
								
								test.java
								
								
								
								
							
							
						
						
									
										15
									
								
								test.java
								
								
								
								
							|  | @ -7,8 +7,10 @@ | ||||||
|  */ |  */ | ||||||
| public class test | public class test | ||||||
| { | { | ||||||
|   public void test(){ |   public static void test(){ | ||||||
| 
 |   Node<String> test = new Node<String>("Hallo"); | ||||||
|  |   Node<Integer> test2 = new Node<Integer>(5); | ||||||
|  |    | ||||||
|  LinkedList liste = new LinkedList(); |  LinkedList liste = new LinkedList(); | ||||||
|  liste.einfuegen(5); |  liste.einfuegen(5); | ||||||
|  liste.einfuegen(6); |  liste.einfuegen(6); | ||||||
|  | @ -16,7 +18,8 @@ public class test | ||||||
|  liste.einfuegen(3); |  liste.einfuegen(3); | ||||||
|  liste.einfuegen(7); |  liste.einfuegen(7); | ||||||
|  liste.einfuegen(9); |  liste.einfuegen(9); | ||||||
|   |  liste.loesche(2); | ||||||
|  |  liste.hinzufügen(8,"wirklich"); | ||||||
|   Node current = liste.first; |   Node current = liste.first; | ||||||
|    |    | ||||||
|   while(current != null){ |   while(current != null){ | ||||||
|  | @ -24,8 +27,10 @@ public class test | ||||||
|     current = current.next; |     current = current.next; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  |   System.out.println("Die Liste hat "+liste.laenge()+" Einträge. "); | ||||||
|    |    | ||||||
|    |   | ||||||
|    | 
 | ||||||
| } | } | ||||||
|  | 
 | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue